source: abuse/branches/win32/Satan Paint/Satan Paint.cpp @ 604

Last change on this file since 604 was 100, checked in by Sam Hocevar, 15 years ago
  • Importing an old Win32 port by Jeremy "Marauder" Scott.
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1// Satan Paint.cpp : Defines the entry point for the application.
2//
3
4#include "stdafx.h"
5#include "resource.h"
6#include "macs.hpp"
7#include "event.hpp"
8#include "jwindow.hpp"
9
10// Foward declarations of functions included in this code module:
11BOOL                            InitInstance(HINSTANCE, int);
12LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
13LRESULT CALLBACK        About(HWND, UINT, WPARAM, LPARAM);
14HWND hWnd;
15HINSTANCE g_hInstance;
16
17int EngineFinished = 0;
18extern int _make_argv(char *p, char ***argv);
19extern char** _argv;
20extern int _argc;
21extern int ErrCode;
22extern char* QuitMessage;
23
24char ExeName[2048];
25start_spaint(int argc, char **argv);
26
27//-----------------------------------------------------------------------------
28unsigned long __stdcall SPaintThread(void* Data)
29{
30    start_spaint(_argc, _argv);
31    EngineFinished = 1;
32    PostMessage(hWnd, WM_SETFOCUS, 0, 0);
33    PostQuitMessage(0);
34    ExitThread(0);
35    return 0;
36}
37
38//-----------------------------------------------------------------------------
39int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
40{
41    DWORD SPaintThreadID = 0;
42    if (!InitInstance(hInstance, nCmdShow))
43        return FALSE;
44    GetModuleFileName(NULL, ExeName, 2048);
45    g_hInstance = hInstance;
46    _argv = (char**) malloc(2 * sizeof(char*));
47        _argv[0] = ExeName;     /* fill in program name */
48        _argc = _make_argv(lpCmdLine, &_argv);
49        _argv[_argc] = NULL;
50   
51    set_mode(19, _argc, _argv); // Set the video mode
52    CreateThread(NULL, NULL, SPaintThread, NULL, NULL, &SPaintThreadID);
53
54        MSG msg;
55        HACCEL hAccelTable;
56
57        hAccelTable = LoadAccelerators(hInstance, (LPCTSTR) IDC_SATANPAINT);
58
59        // Main message loop:
60    while (GetMessage(&msg, NULL, 0, 0) && !EngineFinished) {
61        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
62                        TranslateMessage(&msg);
63                        DispatchMessage(&msg);
64                }
65        }
66    if (EngineFinished)
67        Sleep(100); // Give other threads time to exit; ugly, but it'll work for now
68    set_dprinter(NULL);
69    KillDInput();
70    close_graphics();
71    if (ErrCode)
72        MessageBox(NULL, QuitMessage, "Error", MB_OK);
73
74        ExitProcess(0);
75    return 0;
76}
77
78//-----------------------------------------------------------------------------
79//   FUNCTION: InitInstance(HANDLE, int)
80//
81//   PURPOSE: Saves instance handle and creates main window
82//
83//   COMMENTS:
84//
85//        In this function, we save the instance handle in a global variable and
86//        create and display the main program window.
87//-----------------------------------------------------------------------------
88BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
89{
90    g_hInstance = hInstance; // Store instance handle in our global variable
91
92    WNDCLASS                    wc;
93
94    // Set up and register window class
95    wc.style = CS_HREDRAW | CS_VREDRAW;
96    wc.lpfnWndProc = WndProc;
97    wc.cbClsExtra = 0;
98    wc.cbWndExtra = 0;
99    wc.hInstance = hInstance;
100    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SATANPAINT));
101    wc.hCursor = NULL;
102    wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
103    wc.lpszMenuName = "Satan Paint";
104    wc.lpszClassName = "Satan Paint";
105    RegisterClass(&wc);
106
107    // Create a window
108    hWnd = CreateWindowEx(WS_EX_TOPMOST,
109                          "Satan Paint",
110                          "Satan Paint",
111                          WS_POPUP,
112                          0,
113                          0,
114                          320,
115                          200,
116                          NULL,
117                          NULL,
118                          hInstance,
119                          NULL);
120    if (!hWnd)
121        return FALSE;
122    ShowWindow(hWnd, nCmdShow);
123    UpdateWindow(hWnd);
124    SetFocus(hWnd);
125    SetCursor(NULL);
126    return 1;
127}
128
129//-----------------------------------------------------------------------------
130//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
131//
132//  PURPOSE:  Processes messages for the main window.
133//
134//  WM_COMMAND  - process the application menu
135//  WM_PAINT    - Paint the main window
136//  WM_DESTROY  - post a quit message and return
137//
138//-----------------------------------------------------------------------------
139LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
140{
141    static int focused = 0;
142    switch (message) {
143        case WM_DESTROY:
144            // Clean up and close the app
145            PostQuitMessage(0);
146            return 0L;
147
148        case WM_SETCURSOR:
149            // Turn off the cursor since this is a full-screen app
150            SetCursor(NULL);
151            return 0;
152
153        case WM_SHOWWINDOW:
154            WriteErr(wParam, "Going away");
155            return 0;
156
157        case WM_ACTIVATEAPP:
158            if (wParam == FALSE) {
159                SetDIAcquire = DI_UNACQUIRE_STATE;
160                focused = 0;
161            }
162            else {
163                if (wm && !focused)
164                    wm->push_event(new event(EV_SCREEN_REFRESH));
165                focused = 1;
166                SetDIAcquire = DI_ACQUIRE_STATE;
167            }
168            return 0;
169        case WM_QUIT:
170            PostQuitMessage(wParam);
171            break;
172    }
173    return DefWindowProc(hWnd, message, wParam, lParam);
174}
Note: See TracBrowser for help on using the repository browser.