Jump to content
  • Advertisement
Sign in to follow this  
GotenRulezU

Any idea why this doesn't work?

This topic is 5044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't know why it doesn't work. Could someone who knows more win32 explain plz :) NOTE the text in comment does the same thing as whats in my game loop.
#include<windows.h>
#include<string>
bool gameover = false;
using namespace std;
int scrn[10][10] = {
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1}
};
HBITMAP mybitmap;
/*
void draw(HWND hwnd){
	for(int y = 0; y < 10; y++){
		for(int x = 0; x < 10; x++){
			if(scrn[x][y] == 1){
				mybitmap=(HBITMAP)LoadImage(NULL,"image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
			}
			if(scrn[x][y] == 0){
				mybitmap = (HBITMAP)LoadImage(NULL,"image1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
			}
			SelectObject(imagedc,mybitmap);
			BitBlt(hdc,x*10,y*10,10,10,imagedc,0,0,SRCCOPY);
		}
	}
}
*/
/*void cleanup(HWND hwnd){
	HBITMAP old = (HBITMAP)SelectObject(imagedc,mybitmap);
	DeleteObject(old);
	DeleteDC(imagedc);
	ReleaseDC(hwnd,hdc);
}
*/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	switch(iMsg){
	case WM_DESTROY:
		PostQuitMessage(0);
		gameover = true;
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdline,int iCmd){
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "class";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW;
	RegisterClass(&wc);
	HWND hwnd = CreateWindow("class","my window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
	HDC hdc = GetDC(hwnd);
	HDC imagedc = CreateCompatibleDC(hdc);
	MSG msg;
	while(!gameover){
		if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{
			for(int y = 0; y < 10; y++){
				for(int x = 0; x < 10; x++){
					if(scrn[x][y] == 1){
						mybitmap=(HBITMAP)LoadImage(NULL,"image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
					}
					if(scrn[x][y] == 0){
						mybitmap = (HBITMAP)LoadImage(NULL,"image1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
					}
					SelectObject(imagedc,mybitmap);
					BitBlt(hdc,x*10,y*10,10,10,imagedc,0,0,SRCCOPY);
				}
			}
			
		}
	}
	HBITMAP old = (HBITMAP)SelectObject(imagedc,mybitmap);
	DeleteObject(old);
	DeleteDC(imagedc);
	ReleaseDC(hwnd,hdc);
	UnregisterClass("class",hInstance);
	return 0;
}


Thanks for the help,

Share this post


Link to post
Share on other sites
Advertisement
u need a WM_CLOSE case
the drawing code should be in a WM_PAINT case and started by a InvalidateRect call
u are loading an image file a few 100 times per second

Share this post


Link to post
Share on other sites
Hi,
In the future please be more descriptive with your questions. Its easier for people to help you if you tell us what the problem is. Just a little advice.

Share this post


Link to post
Share on other sites
Yeah my fault, lack of sleep. So instead of loading it inside I should load them to separate bmps outside of the loop and then bitblt whichever one i want to use?

Share this post


Link to post
Share on other sites
u should have an array of images that are loaded when the program starts (display a message so the user knows this is happening) then use the array in the drawing code

u could also do somthing like this:

bitblt(dest,...,imgs[scrn[x][y]],...)

Share this post


Link to post
Share on other sites
Hey why doesn't this work? It just shows a loading cursor and never opens... It should display a map. Am I still going about this wrong?

#include<windows.h>
#include<string>
bool gameover = false;
using namespace std;
int scrn[10][10] = {
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1}
};
HBITMAP mybitmap;
HBITMAP mybitmap1;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
switch(iMsg){
case WM_DESTROY:
PostQuitMessage(0);
gameover = true;
break;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdline,int iCmd){
mybitmap = (HBITMAP)LoadImage(hInstance,"image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
mybitmap1 = (HBITMAP)LoadImage(hInstance,"image1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "class";
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW|CS_HREDRAW;
RegisterClass(&wc);
HWND hwnd = CreateWindow("class","my window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
HDC hdc = GetDC(hwnd);
HDC imagedc = CreateCompatibleDC(hdc);
MSG msg;
while(!gameover){
if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
for(int y = 0; y < 10; y++){
for(int x = 0; x < 10; x++){
if(scrn[x][y] == 1){
SelectObject(imagedc,mybitmap);
}
else{
SelectObject(imagedc,mybitmap1);
}
BitBlt(hdc,x*10,y*10,10,10,imagedc,0,0,SRCCOPY);
}
}

}
}
HBITMAP old = (HBITMAP)SelectObject(imagedc,mybitmap);
DeleteObject(mybitmap);
old = (HBITMAP)SelectObject(imagedc,mybitmap1);
DeleteObject(mybitmap1);
DeleteDC(imagedc);
ReleaseDC(hwnd,hdc);
UnregisterClass("class",hInstance);
return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
u need a WM_CLOSE case
the drawing code should be in a WM_PAINT case and started by a InvalidateRect call
u are loading an image file a few 100 times per second

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!