Any idea why this doesn't work?

Started by
15 comments, last by Rhaal 19 years, 4 months ago
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,
-Goten
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
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.
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?
-Goten
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]],...)
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;}

-Goten
u need a ShowWindow(hwnd,SW_SHOW) after the window creation
Yeah its a paste goof
i Have that in there.
But it shows the window and just shows a loading cursor. Whats wrong?
-Goten
Actually i use ShowWindow(hwnd,iCmd);UpdateWindow(hwnd);
-Goten
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

This topic is closed to new replies.

Advertisement