#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> // DEFINE AND GLOBALS ////////////////////////////////////////////////// #define WINDOW_CLASS_NAME "WINCLASS1" #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) HWND main_window_handle = NULL; HINSTANCE hinstance_app = NULL; char buffer[80]; // Function prototype ////////////////////////////////////////////////// LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM); int Game_Init(void *parms = NULL, int num_parms = 0); int Game_Main(void *parms = NULL, int num_parms = 0); int Game_Shutdown(void *parms = NULL, int num_parms = 0); // WINMAIN //////////////////////////////////////// int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) { WNDCLASSEX winclass; HWND hwnd; MSG msg; // 1. Fill in the window class structure winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // 2. Register the window class if (!RegisterClassEx(&winclass)) return (0); // 3. Create window if (!(hwnd = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "T3D Game Console Version 1.0", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 400, 300, NULL, NULL, hinstance, NULL))) return (0); // Save global handle main_window_handle = hwnd; hinstance_app = hinstance; // Initialize game here Game_Init(); while (TRUE) { // Retrieve message if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } // Main game processing goes here Game_Main(); } // Closedown game here Game_Shutdown(); return msg.wParam; } /////////////////////////////////////////////////////// LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { PAINTSTRUCT ps; HDC hdc; switch(msg) { case WM_CREATE: return (0); case WM_PAINT: hdc = BeginPaint(hwnd, &ps); EndPaint(hwnd, &ps); return (0); case WM_DESTROY: PostQuitMessage(0); return (0); default: break; } return (DefWindowProc(hwnd, msg, wparam, lparam)); } /////////////////////////////////////////////////////// int Game_Init(void *parms, int num_parms) { return (1); } /////////////////////////////////////////////////////// int Game_Main(void *parms, int num_parms) { DWORD start_time = GetTickCount(); // Lock time to 30 fps (1s / 30 = 33ms) while ((GetTickCount() - start_time) < 33); if (KEYDOWN(VK_ESCAPE)) SendMessage(main_window_handle, WM_CLOSE, 0, 0); return (1); } //////////////////////////////////////////////////////// int Game_Shutdown(void *parms, int num_parms) { return (1); }
/////////////////////////////////////////////////////// #define NUM_STARS 256 #define WINDOW_WIDTH 400 #define WINDOW_HEIGHT 300 typedef struct STAR_TYP { int x, y; int vel; COLORREF col; } STAR, *STAR_PTR; HDC global_dc = NULL; STAR stars[NUM_STARS]; void Erase_Stars(void); void Draw_Stars(void); void Move_Stars(void); void Init_Stars(void); /////////////////////////////////////////////////////// int Game_Init(void *parms, int num_parms) { global_dc = GetDC(main_window_handle); Init_Stars(); return (1); } /////////////////////////////////////////////////////// int Game_Main(void *parms, int num_parms) { DWORD start_time = GetTickCount(); Erase_Stars(); Move_Stars(); Draw_Stars(); // Lock time to 30 fps (1s / 30 = 33ms) while ((GetTickCount() - start_time) < 33); if (KEYDOWN(VK_ESCAPE)) SendMessage(main_window_handle, WM_CLOSE, 0, 0); return (1); } //////////////////////////////////////////////////////// int Game_Shutdown(void *parms, int num_parms) { ReleaseDC(main_window_handle, global_dc); return (1); } //////////////////////////////////////////////////////// void Init_Stars(void) { for (int index = 0; index < NUM_STARS; index++) { stars[index].x = rand() % WINDOW_WIDTH; stars[index].y = rand() % WINDOW_HEIGHT; // random velocity 1-16 stars[index].vel = 1 + rand() % 16; int intensity = 15 * (17 - stars[index].vel); stars[index].col = RGB(intensity, intensity, intensity); } } void Erase_Stars(void) { for (int index = 0; index < NUM_STARS; index++) SetPixel(global_dc, stars[index].x, stars[index].y, RGB(0, 0, 0)); } void Draw_Stars(void) { for (int index = 0; index < NUM_STARS; index++) SetPixel(global_dc, stars[index].x, stars[index].y, stars[index].col); } void Move_Stars(void) { for (int index = 0; index < NUM_STARS; index++) { stars[index].x += stars[index].vel; if (stars[index].x >= WINDOW_WIDTH) stars[index].x -= WINDOW_WIDTH; } }