Help Needed - Window won't close

This is a discussion on Help Needed - Window won't close within the C++ Programming forums, part of the General Programming Boards category; I'm still trying out random codes from my Windows Game Programming for Dummies book. I created a program that creates ...

  1. #1
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23

    Problem

    I'm still trying out random codes from my Windows Game Programming for Dummies book. I created a program that creates a window on my Dev C++ compiler and I added the code for a simple spaceship game. It has some errors

    Here is the code:

    Code:
    // INCLUDES FOR WINDOW //////////////
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <windowsx.h>
    #include <stdio.h>
    #include <math.h>
    // INCLUDES FOR GAME ////////////////
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    #include <time.h>
    // DEFINES //////////////////////////
    #define WINDOW_CLASS_NAME "WINCLASS1"
    #define MAX_X   246     // Max X Pos for player
                            // Max Y is 247 
    #define SCROLL_POS 249  // The point that scrolling occurs
    // PROTOTYPES ///////////////////////
    void Init_Graphics(void);
    inline void Set_Color(int fcolor, int bcolor);
    inline void Draw_String(int x, int y, char *string);
    // GLOBALS //////////////////////////
    // save the window handle
    HWND main_window_handle = NULL;
    CONSOLE_SCREEN_BUFFER_INFO con_info; // Holds screen info
    HANDLE hconsole;                     // Handle to Console
    int game_running = 1;                // State of game 1=run 0=done
    // FUNCTIONS ////////////////////////
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
        // this is the main message handler of the system
        PAINTSTRUCT ps;  // used in WM_PAINT
        HDC         hdc; // handle to a device context
        // find out what the message is
        switch(msg)
        {
            case WM_CREATE: // called when window created
            {
                    // do initialization stuff here
                    return(0);
            } break;
            case WM_PAINT: // called when window needs painting
            {
                    // simply validate the window
                    hdc = BeginPaint(hwnd,&ps);
                    EndPaint(hwnd,&ps);
                    return(0);
            } break;
            case WM_DESTROY: // called when window is killed
            {
                    // kill the application
                    PostQuitMessage(0);
                    return(0);
            } break;
        default:break;
        } // end switch
        // process all messages that you didn't take care of
        return (DefWindowProc(hwnd, msg, wparam, lparam));
    } // end WinProc
    void Init_Graphics(void)
    {
        // This initializes the console graphics engine
        COORD console_size = {250,250}; // Size of Console
        // Seed the random number generator with time
        srand((unsigned)time(NULL));
        // Open i/o channel to console screen
        hconsole=CreateFile("CONOUT$",GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        // Make sure we are in 80x25
        SetConsoleScreenBufferSize(hconsole,console_size);
        // Get details for console screen
        GetConsoleScreenBufferInfo(hconsole,&con_info);
    } // end Init_Graphics
    inline void Set_Color(int fcolor, int bcolor=0)
    {
        // This function sets the color of the console output
        SetConsoleTextAttribute(hconsole,(WORD)((bcolor << 4) | fcolor));
    } // End Set_Color
    inline void Draw_String(int x, int y, char *string)
    {
        // This function draws a string at the give x,y
        COORD cursor_pos; // used to pass coords
        // set printing position
        cursor_pos.X = x;
        cursor_pos.Y = y;
        SetConsoleCursorPosition(hconsole,cursor_pos);
        // Print the string in current color
        printf("%s",string);
    } // End Draw_String
    inline void Clear_Screen(void)
    {
        // This function clears the screen
        // Set color to white on black
        Set_Color(15,0);
        // clear the screen
        for (int index=0; index<=50; index++)
            Draw_String(0, SCROLL_POS,"\n");
    } // End Clear_Screen
    // WINMAIN //////////////////////////
    int WINAPI WinMain(HINSTANCE hinstance, 
                       HINSTANCE hprevinstance,
                       LPSTR lpcmdline,
                       int ncmdshow)
    {
        WNDCLASS winclass; // this will hold the class you create
        HWND hwnd;         // generic window handle
        MSG msg;           // generic message
        // first, fill in the window class structure
        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;
        // register the window class
        if (!RegisterClass(&winclass))
            return(0);
        // create the window
        if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,    // class
                     "Title", // Title
                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,  // Flags
                     500,500,                           // x,y
                     250,250,                           // width, height
                     NULL,                              // Handle to parent
                     NULL,                              // Handle to Menu
                     hinstance,                         // instance
                     NULL)))                            // creation parameters
        return(0);
        // save the window handle in a global
        main_window_handle = hwnd;
        // Main Event LOOP
        while(1)
        {
            if (PeekMessage(&msg, NULL,0,0,PM_REMOVE))
            {
                    // test whether this is a quit
                    if (msg.message == WM_QUIT) break;
                    // translate any accelerator keys
                    TranslateMessage(&msg);
                    // send the message to the window proc
            } // end if
                    int main(void)
                    {
                        int xpos = 100;
                        int ypos = 100;
                        // SECTION: initialization
                        // Set up the console text graphics system
                        Init_Graphics();
                        // Clear the screen
                        Clear_Screen();
                        // SECTION: main event loop, this is where all the action
                        // takes place, (the general loop is erase-move-draw)
                        while(game_running)
                            {
                            char key;              // Player input Data
                            int player_x = xpos;     // Player's X position
                            int player_y = ypos;     // Player's Y position
                            // SECTION: erase all objects or clear screen
                            // nothing to erase in our case
                            // SECTION: get player input
                            if (kbhit())
                               {
                               // Get keyboard data, and filter it
                               key = toupper(getch());
                               // Check if player is trying to exit, if so, exit
                               if (key=='Q' || key==27)
                                    game_running=0;
                               // Check if player is moving left
                               if (key=='A')
                                    xpos--;
                               // Check if player is moving right
                               if (key=='D')
                                    xpos++;
                               // Check if player is moving up
                               if (key=='W')
                                    ypos--;
                               // Check if player is moving down
                               if (key=='S')
                                    ypos++;
                               } // end if
                            // SECTION: game logic and futhur processing
                            // make sure player stays on screen
                            if (++xpos > MAX_X)
                                 xpos=0;
                            if (--xpos < 0)
                                 xpos=MAX_X;
                            if (++ypos > 50)
                                 ypos=50;
                            if (--ypos < 0)
                                 ypos=0;
                            // SECTION: draw everything
                            // Draw Player
                            Set_Color(rand()%15,0);
                            Draw_String(xpos,ypos,"<-*->");
                            Draw_String(0,999,"");
                            // SECTION: synchronize to a constant frame rate
                            Sleep(40);
                            } // End while
                        // SECTION: shutdown and bail
                        Clear_Screen();
                    } // End main
        } // end while
    // return to windows like this
    return(msg.wParam);
    } // end WinMain
    The errors are:

    [Warning] In function 'int WinMain(HINSTANCE__*,HINSTNANCE__*,CHAR*, int)':
    parse error before '{' token // this is on line 149
    (Each undeclared identifier is reported only once for each.

    At global scope:
    parse error before 'return' // line 210
    [Build Error] [main.o] Error 1
    Last edited by DZeek; 03-06-2005 at 02:41 PM. Reason: NEW QUESTION

  2. #2
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Question Changed:
    I added some code for a game to run in the window, but errors show up when I compile.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You're trying to mix a console program and a windows GUI program. Just randomly splicing main() inside of WinMain() won't work.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    It could have.
    Then how could it work?

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Sweetie, I don't believe you can have both main() AND WinMain()....you see, WinMain() is the entry point for a Win32 program, and main() is the starting point for a console app....I'm pretty sure you cannot have them both. If that's correct, and you want to have a Win32 app, you should take all your code from main() and stick it into WinMain().

    Besides, you were trying to define a function inside of another one. That ain't right. Can't do that hun.
    Last edited by Krak; 03-06-2005 at 03:54 PM.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>you should take all your code from main() and stick it into WinMain().

    I don't think that will work since the code in main() is specific to a console app...

    I think you're trying to use code that you don't understand. You need to start with something more basic and move on from there. Just copying and pasting code won't help you learn much
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    How would that work? Do I just remove the int main(void) code or what? It's still very confusing to me... I guess I started on something too complicated .

  8. #8
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Anyhoo dear....this might be a shot in the dark...but try using PostQuitMessage(0); in the if(key=='q') part and such to fix the exiting problem. That won't help your compile errors much though.

    I'm not much for Win32...but it seems like something about your return statement seems to be screwy. Anyhoo...it seems like maybe you should hone your skills a tad before diving into the Win32 world, hun. I mean...you had two entry-point functions....and one was even defined inside the other....that's bad news sir. Maybe you should get yourself some more learnin' in, eh?
    Last edited by Krak; 03-06-2005 at 04:04 PM.

  9. #9
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Nah, that doesn't work. I cleared the code of all the console stuff, so it just opens a window. So now lets get back to my original question. When I run my program to create a window, the window appears, but I can't close it. You can't even click on the buttons in the top right corner and alt f4 doesn't work, right clicking and selecting close on th e tab at the bottom of the screen doesn't work. The only way is to use ctrl alt delete, which gets annoying after a while. How can I fix that? Here is the code:

    Code:
    // INCLUDES FOR WINDOW //////////////
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <windowsx.h>
    #include <stdio.h>
    #include <math.h>
    // DEFINES //////////////////////////
    #define WINDOW_CLASS_NAME "WINCLASS1"
    // GLOBALS //////////////////////////
    // save the window handle
    HWND main_window_handle = NULL;
    // FUNCTIONS ////////////////////////
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
        // this is the main message handler of the system
        PAINTSTRUCT ps;  // used in WM_PAINT
        HDC         hdc; // handle to a device context
        // find out what the message is
        switch(msg)
        {
            case WM_CREATE: // called when window created
            {
                    // do initialization stuff here
                    return(0);
            } break;
            case WM_PAINT: // called when window needs painting
            {
                    // simply validate the window
                    hdc = BeginPaint(hwnd,&ps);
                    EndPaint(hwnd,&ps);
                    return(0);
            } break;
            case WM_DESTROY: // called when window is killed
            {
                    // kill the application
                    PostQuitMessage(0);
                    return(0);
            } break;
        default:break;
        } // end switch
        // process all messages that you didn't take care of
        return (DefWindowProc(hwnd, msg, wparam, lparam));
    } // end WinProc
    // WINMAIN //////////////////////////
    int WINAPI WinMain(HINSTANCE hinstance, 
                       HINSTANCE hprevinstance,
                       LPSTR lpcmdline,
                       int ncmdshow)
    {
        WNDCLASS winclass; // this will hold the class you create
        HWND hwnd;         // generic window handle
        MSG msg;           // generic message
        // first, fill in the window class structure
        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;
        // register the window class
        if (!RegisterClass(&winclass))
            return(0);
        // create the window
        if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,    // class
                     "Test", // Title
                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,  // Flags
                     500,500,                           // x,y
                     250,250,                           // width, height
                     NULL,                              // Handle to parent
                     NULL,                              // Handle to Menu
                     hinstance,                         // instance
                     NULL)))                            // creation parameters
        return(0);
        // save the window handle in a global
        main_window_handle = hwnd;
        // Main Event LOOP
        while(1)
        {
            if (PeekMessage(&msg, NULL,0,0,PM_REMOVE))
            {
                    // test whether this is a quit
                    if (msg.message == WM_QUIT) break;
                    // translate any accelerator keys
                    TranslateMessage(&msg);
                    // send the message to the window proc
            } // end if
        } // end while
    // return to windows like this
    return(msg.wParam);
    } // end WinMain

  10. #10
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    You still have a console app running in the background along with the created window. Closing that will kill the window. If I had more time, maybe I could help you out with getting rid of the console app in the background, but I'm sure one of our Win32 whiz-kids will jump in and come to your rescue.

  11. #11
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Bleh, I'm confused with all this stuff.
    I plan to move on to something WAY less difficult. Something which I can mess around with and to use to learn the basics. So, thanks for all your help everybody.

  12. #12
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by DZeek
    Bleh, I'm confused with all this stuff.
    I plan to move on to something WAY less difficult. Something which I can mess around with and to use to learn the basics. So, thanks for all your help everybody.
    A good start would be
    Code:
    #include <iostream>
    int main(void){
         std::cout << "Hello World" << std::endl;
         return 0;
    }
    It uh.....It prints "Hello Word", hun.
    Last edited by Krak; 03-06-2005 at 06:20 PM.

  13. #13
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Quote Originally Posted by Krak
    A good start would be
    Code:
    #include <iostream>
    int main(void){
         std::cout << "Hello World" << std::endl;
         return 0;
    }
    It uh.....It prints "Hello Word", hun.
    Yeah, I kind of figured that out. Sheesh, I'm not that bad at this stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Over Rideing close message when closing window
    By Rune Hunter in forum Windows Programming
    Replies: 8
    Last Post: 12-04-2005, 01:57 PM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 09:39 PM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. Winamp Vis if anyone can help
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-26-2002, 11:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21