Sign in to follow this  
shanelaffond13

Draw problem

Recommended Posts

I have a RPG where I need to draw bitmaps. I have a function to draw bitmaps from files and resources. But, every time I click anything, my program crashes. I am using Win32 API. I am very frustrated with Win32 API and would like any help I can get. Here is my code. main.cpp
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "res.h"
#include "Player.h"
#include "Functions.h"
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

HWND hwnd;               /* This is the handle for our window */

HWND out;

HWND stats;

HWND use;

Plyr Char;

HINSTANCE hThisInstance;

TCHAR Health[64];
TCHAR Mana[64];
TCHAR Gold[64];
TCHAR HP[64];
TCHAR MP[64];
TCHAR Level[64];

void Stats(Plyr Char);  //Shows stats

void Help();  //Show help

void Shutdown();

/*  Make the class name into a global variable  */
char szClassName[ ] = "LOA";

char szWndName[ ] = "Land of Alama";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = 0;                /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default mouse-pointer */
    wincl.hIcon = LoadIcon (hThisInstance, MAKEINTRESOURCE(ico));
    wincl.hIconSm = LoadIcon (hThisInstance, MAKEINTRESOURCE(ico16));
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE(menu);                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = CreateSolidBrush(RGB(0,0,0));

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               szWndName,       /* Title Text */
               WS_MINIMIZEBOX | WS_SYSMENU, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               355,                 /* The programs width */
               580,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
           );

    out = CreateWindow(
              "EDIT",
              "",
              WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_MULTILINE | ES_READONLY,
              10,325,
              325, 140, hwnd, (HMENU)eOut, hThisInstance, NULL);


    stats = CreateWindow(
                "BUTTON",
                "Stats",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                10,475,
                55, 20, hwnd, (HMENU)bStats, hThisInstance, NULL);

    use = CreateWindow(
              "BUTTON",
              "Use",
              WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
              75, 475,
              55, 20, hwnd, (HMENU)bUse, hThisInstance, NULL);


    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT PaintStruct;
    HDC hDC;
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;

        case WM_CREATE:
        {
            // Centering Parent Window
            RECT rect;
            GetWindowRect(hwnd, &rect);
            int xSize = rect.right - rect.left;
            int ySize = rect.bottom - rect.top;
            int x = (GetSystemMetrics(SM_CXSCREEN) - xSize) / 2;
            int y = (GetSystemMetrics(SM_CYSCREEN) - ySize) / 2;
            SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
        }

        break;

        case WM_PAINT:
        hDC = BeginPaint(hwnd, &PaintStruct);
        SetBkMode(hDC, TRANSPARENT);
        SetBkColor(hDC, RGB(0,0,0));
        SetTextColor(hDC, RGB(0,255,0));
        TextOut(hDC, 10, 305, "Output", 6);
        EndPaint(hwnd, &PaintStruct);
        break;

        case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case exit:
            PostMessage(hwnd, WM_CLOSE, 0, 0);
            break;

            case ng:  //New game
            Char.loaded = true;

            Char.setHealth(100);
            Char.setMHealth(100);
            Char.setMana(30);
            Char.setMMana(30);
            Char.setGold(100);
            Char.setHP(1);
            Char.setMP(1);
            Char.setLevel(1);
            Char.setXp(0);

            HWND Out;

            CHAR *n;
            n = "Welcome to the Land of Alama. There is a war of epic proportions going on between the guilds. The thieves guild has been stealing magic from the mages guild, and has been destroying this city. As a soldier for this town, your job is to stop this war.";

            Draw(hThisInstance, hwnd, "Res/char.bmp", 20, 30, 32, 32);

            Out = GetDlgItem(hwnd, eOut);
            CLR(Out);

            Out = GetDlgItem(hwnd, eOut);
            ST(Out, n);
            
            break;

            case bStats:
            
            Out = GetDlgItem(hwnd, eOut);
            CLR(Out);
            if(Char.loaded)
            {
                Stats(Char);
            }
            else
            {
                HWND hOut;

                char *error;

                if(!Char.loaded)
                {
                    hOut = GetDlgItem(hwnd, eOut);

                    error = "There is no current game. Please load a game or start a new one.";

                    ST(hOut, error);
                }
            }
            break;

            case bUse:

            break;

            case load:

            break;
        }

        break;

        default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

void Stats(Plyr Char)
{
    HWND hOut;

    wsprintf(Health,"Your health: %d\r\n", Char.getHealth());
    wsprintf(Mana,"Your mana: %d\r\n", Char.getMana());
    wsprintf(HP,"Your amount of health potions: %d\r\n", Char.getHP());
    wsprintf(MP,"Your amount of mana potions: %d\r\n", Char.getMP());
    wsprintf(Gold,"Your gold: %d\r\n", Char.getGold());
    wsprintf(Level,"Your level: %d", Char.getLevel());

    hOut = GetDlgItem(hwnd, eOut);
    EOF(hOut, "");
    CLR(hOut);

    hOut = GetDlgItem(hwnd, eOut);

    EOF(hOut, Health);

    hOut = GetDlgItem(hwnd, eOut);

    EOF(hOut, Mana);

    hOut = GetDlgItem(hwnd, eOut);

    EOF(hOut, HP);

    hOut = GetDlgItem(hwnd, eOut);

    EOF(hOut, MP);

    hOut = GetDlgItem(hwnd, eOut);

    EOF(hOut, Gold);

    hOut = GetDlgItem(hwnd, eOut);

    EOF(hOut, Level);
}

void Help()
{
    //Not working yet. Need to use dialogs.
}

void Shutdown()
{
    //Nothing to shutdown yet
}


Functions.h

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "res.h"

void Draw(HINSTANCE hThisInstance, HWND hwnd,unsigned short int Name, int x, int y, int sizex, int sizey);  //Basic draw

void Draw(HINSTANCE hThisInstance, HWND hwnd, char *FileName, int x, int y, int sizex, int sizey);  //Basic draw from file


and finally, Functions.cpp


void Draw(HINSTANCE hThisInstance, HWND hwnd,unsigned short int Name, int x, int y, int sizex, int sizey)  //Basic draw from resource
{
	HINSTANCE hDraw = hThisInstance; //Draw instance
    HDC hDCdraw = GetDC(hwnd);  //Create backbuffer DC
    HDC hDCdrawOFF = CreateCompatibleDC( hDCdraw );  //Create backbuffer
    // Find the bitmap resource
	HRSRC hResInfo = FindResource(hDraw, MAKEINTRESOURCE(Name), RT_BITMAP);

	// Load the bitmap resource
	HGLOBAL draw = LoadResource(hDraw, hResInfo);

	HBITMAP hOldBmp = (HBITMAP)SelectObject(hDCdrawOFF, draw);  //Save screen State
    SelectObject(hDCdrawOFF, draw);  //Assign BMP to backbuffer
    GetObject( draw, sizeof(BITMAP), NULL );  //Get BMP

    BitBlt(hDCdraw, x, y, sizex, sizey, hDCdrawOFF, 0, 0, SRCCOPY);
    //Cleanup
    SelectObject(hDCdrawOFF, hOldBmp);
    ReleaseDC( hwnd, hDCdraw );
    DeleteDC( hDCdrawOFF);
    DeleteObject( draw );
}

void Draw(HINSTANCE hThisInstance, HWND hwnd, char *FileName, int x, int y, int sizex, int sizey)  //Basic draw from file
{
    HINSTANCE hDraw = hThisInstance; //Draw instance
    HDC hDCdraw = GetDC(hwnd);  //Create backbuffer DC
    HDC hDCdrawOFF = CreateCompatibleDC( hDCdraw );  //Create backbuffer
    HANDLE draw = LoadImage( hDraw, FileName, IMAGE_BITMAP, sizex, sizey, LR_LOADFROMFILE | LR_CREATEDIBSECTION );  //Load BMP
    HBITMAP hOldBmp = (HBITMAP)SelectObject(hDCdrawOFF, draw);  //Save screen State
    SelectObject(hDCdrawOFF, draw);  //Assign BMP to backbuffer
    GetObject( draw, sizeof(BITMAP), NULL );  //Get BMP

    BitBlt( hDCdraw, x, y, sizex, sizey, hDCdrawOFF, 0, 0, SRCCOPY);  //Draw to window

    //Cleanup
    SelectObject(hDCdrawOFF, hOldBmp);
    ReleaseDC( hwnd, hDCdraw );
    DeleteDC( hDCdrawOFF);
    DeleteObject( draw );
}


The ST and EOF functions are for setting the editbox text and scrolling to the end of the text box respectivly. Please help me. P.S. I HATE WIN32!!! Shane Laffond

Share this post


Link to post
Share on other sites
Posting your entire program isnt very helpful. Where does the program crash? Show us the function and line that the program crashes on. Right now there isnt enough information and there is too much code to sift through.

Share this post


Link to post
Share on other sites
Compile your program in debug mode (if you use VS it is rather easy; if you use devcpp, add -g to your project options), run it in the debugger, and point us the line where the program crash (or, if you are able to understand your problem at this stage, don't forget to tell what the problem was - debugging is difficult, other users might be interested as well by your experience).

Regards,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this