Jump to content
  • Advertisement
Sign in to follow this  
ecr0n

Problems with Microsoft Visual C++

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

y[] I'm having this problem with Microsoft Visual C++(Introductory Edition). Everything seemed to be working fine. I had this simple game up and running that i coded in windows/c++, but my code structure was bad so i decided to start over. Everything seemed fine, so right now i just started with the very basics for the game, a moving paddle at the bottom of the screen, just moving left to right. The problem is that it stops drawing the paddle after about 19000 ticks. I know this because for testing i have the time the game has been running at the very bottom of the screen. Should i post my code? or has anyone had this problem before...?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Yes I had the same problem, except mine was after 19004 ticks.

Share this post


Link to post
Share on other sites
Did you ever find the solution? Any ideas? and where you using the introductory edition as well?

Share this post


Link to post
Share on other sites
main.cpp: just all the window init and including

#define WIN32_LEAN_AND_MEAN
#include "main.h"
#include "func.h"

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch(msg) {
case WM_DESTROY: {
PostQuitMessage(0);
return(0);
} break;

default:break;

}

return (DefWindowProc(hwnd, msg, wparam, lparam));

}

int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow) {

WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
HDC hdc;

winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

hinstance_app = hinstance;

if (!RegisterClassEx(&winclass))
return(0);

if (!(hwnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"Game v0.1",
WS_POPUP|WS_VISIBLE,
WINDOW_START_X,WINDOW_START_Y,
WINDOW_WIDTH,WINDOW_HEIGHT+20,
NULL,
NULL,
hinstance,
NULL)))
return(0);

main_window_handle = hwnd;

Game_Init();

while(TRUE) {
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
if (msg.message == WM_QUIT) { break; }
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Game_Main();
}
Game_Shutdown();

return(msg.wParam);
}




main.h: globals and other declerations

#include <windows.h> // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>

#define WINDOW_WIDTH 200
#define WINDOW_HEIGHT 200
#define WINDOW_START_X 50
#define WINDOW_START_Y 50
#define WINDOW_CLASS_NAME "WINCLASS1"
#define PLAYER_WIDTH 20
#define PLAYER_HEIGHT 5
#define PLAYER_SPEED 1
#define GAME_LAG 6
#define MAX_BULLETS 200
#define BULLET_LENGTH 5
#define UP 1
#define DOWN 0
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

HWND main_window_handle = NULL;
HINSTANCE hinstance_app = NULL;
HDC global_dc;
DWORD start_time,dwStart=GetTickCount();

char buffer[80];
int i;


#include "cplayer.h"
CPLAYER PlayerOne(WINDOW_WIDTH/2-(PLAYER_WIDTH/2),
WINDOW_HEIGHT-PLAYER_HEIGHT,
PLAYER_WIDTH,
PLAYER_HEIGHT,
RGB(0,0,255));



func.h: all the functions and their prototypes

void Game_Main();
void Game_Init();
void Game_Shutdown();
void updateBullets();
void updatePlayers();
void updateHUD();

void Game_Main() {
start_time=GetTickCount();


updateHUD();
updateBullets();
updatePlayers();

SelectObject(global_dc,CreatePen(PS_SOLID,1,RGB(255,255,255)));
MoveToEx(global_dc,0,WINDOW_HEIGHT,NULL);
LineTo(global_dc,WINDOW_WIDTH,WINDOW_HEIGHT);
SelectObject(global_dc,CreatePen(PS_SOLID,1,RGB(0,0,0)));





if (KEYDOWN(VK_ESCAPE)) SendMessage(main_window_handle,WM_CLOSE,0,0);
while(GetTickCount()-start_time<GAME_LAG);
}

void Game_Init() {
global_dc = GetDC(main_window_handle);
}

void Game_Shutdown() {
ReleaseDC(main_window_handle, global_dc);
}

void updateBullets() {

}

void updateHUD() {
SetBkColor(global_dc,RGB(0,0,0));
SetBkMode(global_dc, OPAQUE);

SetTextColor(global_dc,RGB(255,255,255));
sprintf(buffer,": %d points",GetTickCount()-dwStart);
TextOut(global_dc,0,WINDOW_HEIGHT,buffer,strlen(buffer));
}

void updatePlayers() {
if(KEYDOWN(VK_UP)) { }
if(KEYDOWN(VK_DOWN)) { }
if(KEYDOWN(VK_LEFT)) { PlayerOne.vx=-1; }
if(KEYDOWN(VK_RIGHT)) { PlayerOne.vx=+1;}
if(KEYDOWN('w')||KEYDOWN('W')) { } //UP
if(KEYDOWN('s')||KEYDOWN('S')) { } //DOWN
if(KEYDOWN('a')||KEYDOWN('A')) { } //LEFT
if(KEYDOWN('d')||KEYDOWN('D')) { } //RIGHT
PlayerOne.update();
}



cplayer.h: player class

class CPLAYER {
public:
int x,y,vx,width,height;
COLORREF color;

CPLAYER(int new_x, int new_y, int new_width, int new_height, COLORREF new_color) {
x=new_x;
y=new_y;
width=new_width;
height=new_height;
color=new_color;
}

void move() {
x+=vx;
}

void draw() {
SelectObject(global_dc,CreateSolidBrush(color));
Rectangle(global_dc,x,y,x+width,y+height);
SelectObject(global_dc,CreateSolidBrush(RGB(0,0,0)));
}

void erase() {
SelectObject(global_dc,CreateSolidBrush(RGB(0,0,0)));
Rectangle(global_dc,x,y,x+width,y+height);
SelectObject(global_dc,CreateSolidBrush(RGB(0,0,0)));
}

void Collision_window() {
if(x<0) { x=0; }
if(x+width>WINDOW_WIDTH) { x=WINDOW_WIDTH; }
}

void update() {
erase();
move();
Collision_window();
draw();
}
};

]

Share this post


Link to post
Share on other sites
Right here:

//GameMain()
SelectObject(global_dc,CreatePen(PS_SOLID,1,RGB(255,255,255)));
MoveToEx(global_dc,0,WINDOW_HEIGHT,NULL);
LineTo(global_dc,WINDOW_WIDTH,WINDOW_HEIGHT);
SelectObject(global_dc,CreatePen(PS_SOLID,1,RGB(0,0,0)));

//cplayer.draw()
SelectObject(global_dc,CreateSolidBrush(color));
Rectangle(global_dc,x,y,x+width,y+height);
SelectObject(global_dc,CreateSolidBrush(RGB(0,0,0)));



Each call to CreatePen creates a pen(suprise), but that pen is never destroyed. This means that after 19000 frames, you have 38000 pens. At that point, windows decides not to give you any more pens. The same happens with brushes. You could destroy brushes and pens when you finish with them, but I think the best thing for this application would be to create them once and select them when you need them.


[/source]

Share this post


Link to post
Share on other sites
Alright, thx alot. I thought that by creating it within the SelectObject it would just destroy itself.. well not destroy itself, i don't know the proper term but it was bascially just temporary. Well Thanks alot, this helps greatly!

Share this post


Link to post
Share on other sites
Ya, i plan to. I took a look at the DirectX and COM chapter and i was like whoa, i think i'm gonna fool around a bit. So i'm just gonna do a couple of small proggys with Windows then move on. :D.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!