Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

SSJCORY

I need tips on my program!

This topic is 5480 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

This is my first ever windows program... You get to move a red block around yay!!! I need some tips to make it better. I''m only throught the first 10 pages of the windows section of my book so this is what i''ve learned thus far. TIPS ARE NEEDED! CRITISISM ACCEPTED!Here it is:
#include<windows.h>
#include<stdio.h>
bool paintred = false;
bool paintblue = false;
bool down = false;
bool up = false;
bool left = false;
bool right = false;
int WIN_WIDTH = 958;
int WIN_HEIGHT = 715;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprev,PSTR szCmdLine, int iCmdShow){
	HWND hwnd;
	MSG msg;
	WNDCLASSEX flash;
	flash.cbSize = sizeof(flash);
	flash.lpfnWndProc = WndProc;
	flash.hInstance = hInstance;
	flash.hIcon = LoadIcon(NULL,IDI_WINLOGO);
	flash.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
	flash.hCursor = LoadCursor(NULL,IDC_ARROW);
	flash.lpszClassName = "Cory Class";
	flash.lpszMenuName = 0;
	flash.cbClsExtra = 0;
	flash.cbWndExtra = 0;
	flash.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	flash.style = CS_HREDRAW | CS_VREDRAW;

	RegisterClassEx(&flash);
	hwnd = CreateWindow("Cory Class", "Cory''s Window", WS_OVERLAPPEDWINDOW, 0, 
		0, WIN_WIDTH, WIN_HEIGHT, NULL, NULL, hInstance, NULL);
	HDC hdc = GetDC(hwnd);
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);
	RECT rect = {0,0,50,50};
	RECT killold = {0,0,50,50};

	HBRUSH RED = CreateSolidBrush(COLORREF RGB(255,0,0));
	HBRUSH BLUE = CreateSolidBrush(COLORREF RGB(0,0,255));
	FillRect(hdc,&rect,RED);
	while(GetMessage(&msg,NULL,0,0)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);

		FillRect(hdc,&rect,RED);
		if(down == true && (rect.bottom + 100) <= WIN_HEIGHT){
			rect.bottom += 100;
			rect.top += 100;
			FillRect(hdc,&rect,RED);
			FillRect(hdc,&killold,WHITE_BRUSH);
			killold.bottom += 100;
			killold.top += 100;
		}
		if(up == true && (rect.top - 50) > 0){
			rect.bottom -= 100;
			rect.top -= 100;
			FillRect(hdc,&rect,RED);
			FillRect(hdc,&killold,WHITE_BRUSH);
			killold.top -= 100;
			killold.bottom -= 100;
		}
		if(left == true && (rect.left - 50) > 0){
			rect.left -= 100;
			rect.right -= 100;
			FillRect(hdc,&rect,RED);
			FillRect(hdc,&killold,WHITE_BRUSH);
			killold.left -= 100;
			killold.right -= 100;
		}
		if(right == true && (rect.right + 100) <= WIN_WIDTH){
			rect.left += 100;
			rect.right += 100;
			FillRect(hdc,&rect,RED);
			FillRect(hdc,&killold,WHITE_BRUSH);
			killold.right += 100;
			killold.left += 100;
		}

		up = false;
		down = false;
		left = false;
		right = false;
	}
	ReleaseDC(hwnd,hdc);
	UnregisterClass("Cory Class", hInstance);
	return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam){
	switch(iMsg){
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	case WM_CHAR:
		{
			TCHAR INPUT = (TCHAR)wParam;
			if(INPUT == ''8''){
				up = true;
			}
			if(INPUT == ''2''){
				down = true;
			}
			if(INPUT == ''4''){
				left = true;
			}
			if(INPUT == ''6''){
				right = true;
			}
		}
	}
	return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.|

Share this post


Link to post
Share on other sites
Advertisement
<< TIPS ARE NEEDED! CRITISISM ACCEPTED! >>

My criticism: stop posting so many damn messages.

Anyway, you''re doing fine. Do you need a cheerleader? Okay, rah rah!

My tip: Probably the next step is moving other things around, like a bitmap with Win GDI. Also learn double or back buffering for smooth animation (basically you set up a compatible dc full screen). The cool thing with double buffering is you don''t need to worry about "erasing" your objects individually, since they are all wiped clean each frame (the whole screen is erased).

My demos (lower right) show how to set up a double buffer, and move bitmaps.

VazGames.com

Now make a game with those stupid rectangles! You''re not that far behind me, I just started early this year with Windows, although I''ve been programming over 20 years.

Phil P

Share this post


Link to post
Share on other sites
Thnx man THat cheerleader brightens my day i been a little frustrated not being able to get windows to do some of the things i want it to.



Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.|

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by SSJCORY
Thnx man THat cheerleader brightens my day i been a little frustrated not being able to get windows to do some of the things i want it to.

<hr>
<font face="" color="Blue">Favorite Quotes:</font><font face="" color="red">Gandalf: You shall not pass!</font>|<font face="" color="orange">Gollum: My Precious!</font>|<font face="" color="violet">Smeagol: My little hobbitses.</font>|
<hr>


Yeah programming is constantly a frustrating experience, so you better get used to it. My advice is to practice yoga or meditation, or at least buy one of those squishy stress balls.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
suggestions:
o convert all of those if tests in the WM_CHAR processing to a switch statement

o get rid of the killold rect use. you don''t need 2 rects to do what you''re doing

o notice how all of the code within the "if(down == true", "if(up == true", etc., statements looks almost the same? take the code from within one of those if statements and make a function out of it so that it can be used with all 4 ifs

o every GDI object that is created needs to be destroyed or a GDI memory leak will occur. so put a couple of DeleteObject calls at the end of WinMain for the 2 brushes that are created

o you still have a problem with the "up" and "left" if statements - why didn''t you change them like you changed the "down" and "right" ifs?

o change the 100 numeric to be a variable instead. then add code in your WM_CHAR processing so that you can increase and decrease the step size the rect takes. this will show you if you''ve come up with a window boundary solution that works for all cases

o breaking the "up", "down", etc. key input processing into two different pieces of code is not necessary. so either move all of the ifs you have in WinMain down into the WndProc or do away with the WM_CHAR processing and use GetAsyncKeyState instead. this will also eliminate the need for those global bools currently used

o if you''re up for it, convert all of that code that moves/paints the rect into a class and then make use of that class to create both a red and a blue rect that can be moved around independently of each other

Share this post


Link to post
Share on other sites
I did everything above accept the making rectangle class, GetAsyncKey() and getting rid of killold. Because i dont know how to do the last two and i''m to tired right now to make a class.
Here''s the update:

#include<windows.h>
#include<stdio.h>
bool paintred = false;
bool paintblue = false;
bool down = false;
bool up = false;
bool left = false;
bool right = false;
int step = 100;
int WIN_WIDTH = 650;
int WIN_HEIGHT = 600;
HWND hwnd;
HDC hdc = GetDC(hwnd);
RECT rect = {100,100,50,50};
RECT killold = {100,100,50,50};
RECT rightborder = {(WIN_WIDTH - 10), 10, (WIN_HEIGHT - 10),10};
HBRUSH RED = CreateSolidBrush(COLORREF RGB(255,0,0));
HBRUSH BLUE = CreateSolidBrush(COLORREF RGB(0,0,255));
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprev,PSTR szCmdLine, int iCmdShow){
MSG msg;
WNDCLASSEX flash;
flash.cbSize = sizeof(flash);
flash.lpfnWndProc = WndProc;
flash.hInstance = hInstance;
flash.hIcon = LoadIcon(NULL,IDI_WINLOGO);
flash.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
flash.hCursor = LoadCursor(NULL,IDC_ARROW);
flash.lpszClassName = "Cory Class";
flash.lpszMenuName = 0;
flash.cbClsExtra = 0;
flash.cbWndExtra = 0;
flash.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
flash.style = CS_HREDRAW | CS_VREDRAW;

RegisterClassEx(&flash);
hwnd = CreateWindow("Cory Class", "Cory''s Window", WS_OVERLAPPEDWINDOW, 0,
0, WIN_WIDTH, WIN_HEIGHT, NULL, NULL, hInstance, NULL);

ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
FillRect(hdc,&rect,RED);
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ReleaseDC(hwnd,hdc);
UnregisterClass("Cory Class", hInstance);
DeleteObject(RED);
DeleteObject(BLUE);
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam){
switch(iMsg){
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_CHAR:
{
TCHAR INPUT = (TCHAR)wParam;
switch(INPUT){

case ''8'':{
up = true;
break;
}

case ''2'':{
down = true;
break;
}
case ''4'':{
left = true;
break;
}
case ''6'':{
right = true;
break;
}
}
}
}
FillRect(hdc,&rect,RED);
FillRect(hdc,&rightborder,BLUE);
if(down == true && (rect.bottom + step) < WIN_HEIGHT){
rect.bottom += step;
rect.top += step;
FillRect(hdc,&rect,RED);
FillRect(hdc,&killold,WHITE_BRUSH);
killold.bottom += step;
killold.top += step;
}
if(up == true && (rect.top - step) > 0){
rect.bottom -= step;
rect.top -= step;
FillRect(hdc,&rect,RED);
FillRect(hdc,&killold,WHITE_BRUSH);
killold.top -= step;
killold.bottom -= step;
}
if(left == true && (rect.left - step) > 0){
rect.left -= step;
rect.right -= step;
FillRect(hdc,&rect,RED);
FillRect(hdc,&killold,WHITE_BRUSH);
killold.left -= step;
killold.right -= step;
}
if(right == true && (rect.right + step) < WIN_WIDTH){
rect.left += step;
rect.right += step;
FillRect(hdc,&rect,RED);
FillRect(hdc,&killold,WHITE_BRUSH);
killold.right += step;
killold.left += step;
}

up = false;
down = false;
left = false;
right = false;
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

ANY MORE TIPS WOULD BE GREATLY APRECIATED!



Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.|

Share this post


Link to post
Share on other sites
Update that was the old version here is the new one

#include<windows.h>
#include<stdio.h>
int step = 100;
int WIN_WIDTH = 650;
int WIN_HEIGHT = 600;
HWND hwnd;
HDC hdc = GetDC(hwnd);
RECT rect = {100,100,50,50};
RECT killold = {100,100,50,50};
HBRUSH RED = CreateSolidBrush(COLORREF RGB(255,0,0));
HBRUSH BLUE = CreateSolidBrush(COLORREF RGB(0,0,255));
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprev,PSTR szCmdLine, int iCmdShow){
MSG msg;
WNDCLASSEX flash;
flash.cbSize = sizeof(flash);
flash.lpfnWndProc = WndProc;
flash.hInstance = hInstance;
flash.hIcon = LoadIcon(NULL,IDI_WINLOGO);
flash.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
flash.hCursor = LoadCursor(NULL,IDC_ARROW);
flash.lpszClassName = "Cory Class";
flash.lpszMenuName = 0;
flash.cbClsExtra = 0;
flash.cbWndExtra = 0;
flash.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
flash.style = CS_HREDRAW | CS_VREDRAW;

RegisterClassEx(&flash);
hwnd = CreateWindow("Cory Class", "Cory''s Window", WS_OVERLAPPEDWINDOW, 0,
0, WIN_WIDTH, WIN_HEIGHT, NULL, NULL, hInstance, NULL);

ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
FillRect(hdc,&rect,RED);
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ReleaseDC(hwnd,hdc);
UnregisterClass("Cory Class", hInstance);
DeleteObject(RED);
DeleteObject(BLUE);
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam){
switch(iMsg){
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_CHAR:
{
TCHAR INPUT = (TCHAR)wParam;
switch(INPUT){

case ''8'':{
if((rect.top - step) > 0){
rect.bottom -= step;
rect.top -= step;
FillRect(hdc,&rect,RED);
FillRect(hdc,&killold,WHITE_BRUSH);
killold.top -= step;
killold.bottom -= step;
}
break;
}

case ''2'':{
if((rect.bottom + step) < WIN_HEIGHT){
rect.bottom += step;
rect.top += step;
FillRect(hdc,&rect,RED);
FillRect(hdc,&killold,WHITE_BRUSH);
killold.bottom += step;
killold.top += step;
}
break;
}
case ''4'':{
if((rect.left - step) > 0){
rect.left -= step;
rect.right -= step;
FillRect(hdc,&rect,RED);
FillRect(hdc,&killold,WHITE_BRUSH);
killold.left -= step;
killold.right -= step;
}
break;
}
case ''6'':{
if((rect.right + step) < WIN_WIDTH){
rect.left += step;
rect.right += step;
FillRect(hdc,&rect,RED);
FillRect(hdc,&killold,WHITE_BRUSH);
killold.right += step;
killold.left += step;
}
break;
}
}
}
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

Here we go



Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.|

Share this post


Link to post
Share on other sites
Better than stress balls, or yoga or something,... Take up the fine art of reproduction or medieval/Ren armor. Nothing better than bashing steel into a helm or breastplate.

Check out www.arador.com for more info on a great, fun form of stress relife. (Just be careful that you don''t end up having to code stuff to help rid yourself of stress from armoring, ;0 cause then you just have a problem)

Share this post


Link to post
Share on other sites
Or: EnterTheNinja.com

Anyways, you''ve inspired me to keep on studying for my Spanish, Gemoetry, Biologoy, and Orchestra test I have this Thursday! (BDW, I"m gonna start where I left of in Windows again! )

Good luck to you and me!



WiseElben.com | My Journal | My Profile
[edited by - GameDev Staff on September 27, 1989 9X:58:97 XMS] For violating Article 43 Page 456 Paragraph 251 Line .042]

Share this post


Link to post
Share on other sites

  • 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!