Skip to content

Instantly share code, notes, and snippets.

@Gelio
Created March 8, 2017 09:52
Show Gist options
  • Save Gelio/978127a21d10e3b17f435cb0be7c3331 to your computer and use it in GitHub Desktop.
Save Gelio/978127a21d10e3b17f435cb0be7c3331 to your computer and use it in GitHub Desktop.
// lab1-cs.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "lab1-cs.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
ATOM RegisterBallClass(HINSTANCE hInstance);
ATOM RegisterPaddleClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK BallWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK PaddleWndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_LAB1CS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
RegisterBallClass(hInstance);
RegisterPaddleClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAB1CS));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB1CS));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush(RGB(255, 242, 0));
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_LAB1CS);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
ATOM RegisterBallClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = BallWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB1CS));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush(RGB(255, 0, 0));
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_LAB1CS);
wcex.lpszClassName = _T("ball");
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
ATOM RegisterPaddleClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = PaddleWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB1CS));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_ACTIVECAPTION);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_LAB1CS);
wcex.lpszClassName = _T("paddle");
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
int screenWidth = GetSystemMetrics(SM_CXSCREEN),
screenHeight = GetSystemMetrics(SM_CYSCREEN);
mainWindow = CreateWindowW(szWindowClass, szTitle, (WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX),
(screenWidth - 200) / 2, (screenHeight - 300) / 2, 200, 300, nullptr, nullptr, hInstance, nullptr);
if (!mainWindow)
{
return FALSE;
}
SetWindowLong(mainWindow, GWL_EXSTYLE,
GetWindowLong(mainWindow, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TOPMOST);
SetLayeredWindowAttributes(mainWindow, 0, 255 * 0.2, LWA_ALPHA);
SetWindowPos(mainWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
ShowWindow(mainWindow, nCmdShow);
UpdateWindow(mainWindow);
ball = CreateWindowW(_T("ball"), _T("hello"), WS_CHILDWINDOW, 0, 0, 10, 10, mainWindow, nullptr, hInstance, nullptr);
if (!ball)
return FALSE;
SetLayeredWindowAttributes(ball, 0, 255, LWA_ALPHA);
HRGN region = CreateEllipticRgn(0, 0, 10, 10);
SetWindowRgn(ball, region, true);
ShowWindow(ball, nCmdShow);
UpdateWindow(ball);
paddle = CreateWindowW(_T("paddle"), _T("hello"), WS_CHILDWINDOW, 40, 232, 40, 10, mainWindow, nullptr, hInstance, nullptr);
if (!paddle)
return FALSE;
SetLayeredWindowAttributes(paddle, 0, 255, LWA_ALPHA);
ShowWindow(paddle, nCmdShow);
UpdateWindow(paddle);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
void MovePaddle(int mouseX, int mouseY)
{
POINT mousePos;
mousePos.x = mouseX;
mousePos.y = mouseY;
int maxPossibleMouseX = WINDOW_WIDTH - PADDLE_WIDTH / 2;
if (mousePos.x > maxPossibleMouseX)
mousePos.x = maxPossibleMouseX;
if (mousePos.x - PADDLE_WIDTH / 2 < 0)
mousePos.x = PADDLE_WIDTH / 2;
RECT paddleRect;
GetWindowRect(paddle, &paddleRect);
POINT topLeft;
topLeft.x = paddleRect.left;
topLeft.y = paddleRect.top;
ScreenToClient(mainWindow, &topLeft);
MoveWindow(paddle, mousePos.x - PADDLE_WIDTH / 2, topLeft.y, PADDLE_WIDTH, PADDLE_HEIGHT, TRUE);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
RECT rect;
GetClientRect(hWnd, &rect);
windowInsideWidth = rect.right - rect.left;
windowInsideHeight = rect.bottom - rect.top;
SetLayeredWindowAttributes(hWnd, NULL, 255 * 0.2, LWA_ALPHA);
UpdateWindow(hWnd);
}
break;
case WM_MOUSEMOVE:
if (gameRunning)
MovePaddle(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case ID_ACCELERATOR32771:
PostQuitMessage(0);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void MoveBall()
{
RECT ballRect;
GetWindowRect(ball, &ballRect);
POINT ballTopLeft;
ballTopLeft.x = ballRect.left;
ballTopLeft.y = ballRect.top;
ScreenToClient(mainWindow, &ballTopLeft);
if (ballTopLeft.y >= windowInsideHeight - PADDLE_HEIGHT - BALL_HEIGHT)
{
// Odbicie lub nie, w zależności gdzie jest paletka
RECT paddleRect;
GetWindowRect(paddle, &paddleRect);
POINT paddleTopLeft;
paddleTopLeft.x = paddleRect.left;
paddleTopLeft.y = paddleRect.top;
ScreenToClient(mainWindow, &paddleTopLeft);
if (paddleTopLeft.x <= ballTopLeft.x && paddleTopLeft.x + PADDLE_WIDTH >= ballTopLeft.x)
ballSpeedY = -ballSpeedY;
else
{
gameRunning = false;
}
}
if (ballTopLeft.y < 0)
ballSpeedY = -ballSpeedY;
if (ballTopLeft.x < 0)
ballSpeedX = -ballSpeedX;
if (ballTopLeft.x >= windowInsideWidth - BALL_WIDTH)
ballSpeedX = -ballSpeedX;
MoveWindow(ball, ballTopLeft.x + ballSpeedX, ballTopLeft.y + ballSpeedY, BALL_WIDTH, BALL_HEIGHT, TRUE);
}
LRESULT CALLBACK BallWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
SetTimer(hWnd, 0, 50, NULL);
break;
case WM_TIMER:
if (gameRunning)
MoveBall();
else
KillTimer(hWnd, NULL);
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK PaddleWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment