Sign in to follow this  
dustydoodoo

What is wrong with my code? (C++)

Recommended Posts

dustydoodoo    100
Well, ive done this kind of double buffer thing before but i made a new program to just test out all my new functions, and my Borland compiler come with this error "Case bypasses initializtion of a local variable", and the line of code that it says is errored is this case WM_DESTROY: DeInit(&Buffer); break; and the functions DeInit is void DeInit(BUFFER *buffer) { SelectObject(buffer->Back, buffer->hOldBitmap); DeleteDC(buffer->Back); DeleteDC(buffer->Bitmap); PostQuitMessage(0); }; And i dont know if that is the errored part, but please look at it and if it is fine, please make a suggestion of what is probably wrong, or tell me what the compiler means.

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Quote:
Original post by dustydoodoo
Borland compiler come with this error "Case bypasses initializtion of a local variable", and the line of code that it says is errored is this


That error means that somewhere in your switch statement, you make a local variable. It is just warning you of this. If you can post your entire switch statement using the [source] [/source] tags, I'm sure we can point out where it is happening at.

Share this post


Link to post
Share on other sites
dustydoodoo    100

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wparam, LPARAM lparam)
{
switch(iMsg){

case WM_CREATE:
HBITMAP Bit = LoadBitmap("Bitmap.bmp");
DisplayBitmap(&Buffer, Bit, 100, 100);
SwapBuffers(&Buffer);
Init(&Buffer, hwnd);
break;

case WM_DESTROY:
DeInit(&Buffer);
PostQuitMessage(0);
break;
}
return DefWindowProc (hwnd, iMsg, wparam, lparam);
}

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Yeap:
HBITMAP Bit = LoadBitmap("Bitmap.bmp");
is what's causing that warning. Ideally, it should be a global variable to you can free the resources allocated to it at the end of your program. So you can just do this:

// Top of program
HBITMAP Bit;

// In that code
Bit = LoadBitmap("Bitmap.bmp");


The error should go away when you make those changes.

Share this post


Link to post
Share on other sites
garyfletcher    250
or try:


LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wparam, LPARAM lparam)
{
switch(iMsg){

case WM_CREATE:
{
HBITMAP Bit = LoadBitmap("Bitmap.bmp");
DisplayBitmap(&Buffer, Bit, 100, 100);
SwapBuffers(&Buffer);
Init(&Buffer, hwnd);
}
break;

case WM_DESTROY:
DeInit(&Buffer);
PostQuitMessage(0);
break;
}
return DefWindowProc (hwnd, iMsg, wparam, lparam);
}





or


LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wparam, LPARAM lparam)
{
switch(iMsg){

case WM_CREATE:
DisplayBitmap(&Buffer, LoadBitmap("Bitmap.bmp"), 100, 100);
SwapBuffers(&Buffer);
Init(&Buffer, hwnd);
break;

case WM_DESTROY:
DeInit(&Buffer);
PostQuitMessage(0);
break;
}
return DefWindowProc (hwnd, iMsg, wparam, lparam);
}



try to avoid globals where ever possible. Although in this case it may be a good idea if you are loading Bitmap.bmp in several place. Using a global in this instance would mean that you could load it in once, in some kind of initialisation function.

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Quote:
Original post by garyfletcher
or try:
*** Source Snippet Removed ***
or
*** Source Snippet Removed ***


While that is a good suggestion, consider this from MSDN:

Quote:
The application must call the DeleteObject function to delete each bitmap handle returned by the LoadBitmap function.


The only other way to get around this would be if you made a class that contained a HBITMAP, to which is a lot more work than just having one global variable wrapped in a namespace at the top [smile]. Not only that, the function call:

DisplayBitmap(&Buffer, LoadBitmap("Bitmap.bmp"), 100, 100);

should end up being called in more places than just the WM_CREATE message, so either a class or a global would have to be used regardless.

Share this post


Link to post
Share on other sites
dustydoodoo    100
Hey, thanks (the person who said to make it a global variable) IT WORKS, ITS ALIVE!!! lol, thanks, oh and what kind of resorses are alocated to the bitmap? and and you said, "you can free the resources allocated to it at the end of your program." well how "can" i, or does it just do it it self? that might sound stupid, but im really tired.

Share this post


Link to post
Share on other sites
Drew_Benton    1861
You will want to do what Garyfletcher and myself have agreed is the better way [wink]. Something like this (kind of messy, but quick example):

struct cData
{
HBITMAP Bit;

public:
cData();

~cData()
{
DeleteObject(Bit);
}

void Load( std::string name )
{
Bit = LoadBitmap( name );
}
};


At the top you will have something like:
cData g_MyData;

Then in your initialize code, you will have:
g_MyData.Load("Bitmap.bmp")

Then in your draw:
DisplayBitmap(&Buffer, g_MyData.Bit, 100, 100);

If you see in the dtor, there is a call to DeleteObject, so it will do an auto cleanup. You might also want to consider using this with your Buffer variable as well so you can wrap everything up!


struct cData
{
HBITMAP Bit;
BUFFER *buffer;

public:
cData() { };
~cData() { };

void Destroy()
{
SelectObject(buffer->Back, buffer->hOldBitmap);
DeleteDC(buffer->Back);
DeleteDC(buffer->Bitmap);
DeleteObject(Bit);
}

void Load( std::string name )
{
Bit = LoadBitmap( name );
}

void Draw( int x, int y)
{
DisplayBitmap(&Buffer, Bit, X, Y);
SwapBuffers(&Buffer);
}
};
...
case WM_CREATE:
{
g_MyData.Draw( 100, 100 );
Init(&Buffer, hwnd); // will need to fix this
}
case WM_DESTROY:
g_MyData.Destroy();



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