Jump to content
  • Advertisement
Sign in to follow this  
SSJCORY

Problem textout with strings.

This topic is 4721 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 used .c_str() in the textout function and also .size() for the size. Why doesnt it work? It compiles fine but i get an access violation?!? i dont konw why here is my code.
#include<windows.h>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
class cMap;
class cPlayer;
class cMap{
private:
	int width;
	int height;
	vector<cPlayer> players;
	int currentplayer;
public:
	int getwidth(){
		return width;
	}
	int getheight(){
		return height;
	}
	cMap(){
		width = 500;
		height = 500;
		currentplayer = 0; 
	}
	cMap(int newwidth,int newheight){
		width = newwidth;
		height = newheight;
		currentplayer = 0;
	}
	void moveplayer(int direction);
	void addplayer(cPlayer newplayer);
	void display(HWND hwnd);
};
class cPlayer{
private:
	int x;
	int y;
	int life;
	string name;
	HBITMAP visual;
public:
	int getX(){
		return x;
	}
	int getY(){
		return y;
	}
	HBITMAP getVisual(){
		return visual;
	}
	void setX(int newx){
		x = newx;
	}
	void setY(int newy){
		y = newy;
	}
	void setVisual(HBITMAP newbitmap){
		visual = newbitmap;
	}
	cPlayer(){
		visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
		x = 0;
		y = 0;
		name = "default";
		life = 100;
	}
	cPlayer(int startx,int starty,string newname){
		visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
		x = startx;
		y = starty;
		name = newname;
		life = 100;
	}
	cPlayer(HBITMAP hbitmap){
		visual = hbitmap;
		x = 0;
		y = 0;
		name = "default";
		life = 100;
	}
	cPlayer(HBITMAP hbitmap,int startx,int starty){
		visual = hbitmap;
		x = startx;
		y = starty;
		name = "default";
		life = 100;
	}
	void display(HWND hwnd){
		HDC hdc = GetDC(hwnd);
		HDC idc = CreateCompatibleDC(hdc);
		SelectObject(idc,visual);
		BitBlt(hdc,x,y,50,50,idc,0,0,SRCCOPY);
		ostringstream text;
		text<<life;
		TextOut(hdc,x-15,y-70,"--------------",14);
		TextOut(hdc,x-17,y-70,"+",1);
		TextOut(hdc,x-17,y-40,"+",1);
		TextOut(hdc,x-17,y-60,"+",1);
		TextOut(hdc,x,y-40,text.str().c_str(),text.str().size());
		TextOut(hdc,x,y-100,name.c_str(),name.size());///////////////////PROBLEM WITH NAMES????
		DeleteDC(idc);
	}
	
};
cMap cmap;
bool done = false;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	switch(iMsg){
	case WM_DESTROY:
		PostQuitMessage(0);
		done = true;
		return 0;
		break;
	case WM_KEYDOWN:
		RECT rect;
		HDC hdc = GetDC(hwnd);
		GetWindowRect(hwnd,&rect);
		FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
		switch(wParam){
		case VK_LEFT:
			cmap.moveplayer(1);
			break;
		case VK_RIGHT:
			cmap.moveplayer(2);
			break;
		case VK_UP:
			cmap.moveplayer(3);
			break;
		case VK_DOWN:
			cmap.moveplayer(4);
			break;
		}
		cmap.display(hwnd);
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdLine,int iCmd){
	HWND hwnd;
	MSG msg;
	WNDCLASS wc;
	wc.cbClsExtra = NULL;
	wc.cbWndExtra = NULL;
	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 = "cory";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW;
	RegisterClass(&wc);
	hwnd = CreateWindow("cory","window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,iCmd);
	UpdateWindow(hwnd);
	cPlayer player(200,200,"Kara");
	cPlayer player2(300,300,"Cory");
	
	cmap.addplayer(player);
	cmap.addplayer(player2);

	while(!done){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{
		}
	}
	UnregisterClass("cory",hInstance);
	return 0;
}
void cMap::display(HWND hwnd){
	HDC hdc = GetDC(hwnd);
	ostringstream os;
	os<<currentplayer;
	TextOut(hdc,10,10,"Player ",7);
	TextOut(hdc,60,10,os.str().c_str(),os.str().size());
	int countdown = players.size();
	while(countdown >= 0){
		players[countdown].display(hwnd);
		countdown--;
	}
}
void cMap::addplayer(cPlayer newplayer){
	players.push_back(newplayer);
}
void cMap::moveplayer(int direction){
	if(direction == 1){
		players[currentplayer].setX(players[currentplayer].getX()-50);
	}
	if(direction == 2){
		players[currentplayer].setX(players[currentplayer].getX()+50);
	}
	if(direction == 3){
		players[currentplayer].setY(players[currentplayer].getY()-50);
	}
	if(direction == 4){
		players[currentplayer].setY(players[currentplayer].getY()+50);
	}
	if(currentplayer >= players.size() -1){
		currentplayer = 0;
	}
	else{
		currentplayer++;
	}
}

Thanks, Cory

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Kylotan
Does it run ok if you comment out those last 2 TextOut lines then?


It shouldn't:


int countdown = players.size();
while(countdown >= 0){
players[countdown].display(hwnd);
countdown--;
}


indeces start at 0
It's been 3 threads now.

Share this post


Link to post
Share on other sites
oh i see what you mean now duh WHY THE FUCK DO THEY START AT ZERO AND NOT FACTOR THAT INTO THE SIZE?? Llololol
Thanks again man god i feel dumb,
COry

Share this post


Link to post
Share on other sites
Quote:
Original post by SSJCORY
oh i see what you mean now duh WHY THE FUCK DO THEY START AT ZERO AND NOT FACTOR THAT INTO THE SIZE?? Llololol


Because they intend to imitate the interface of a plain array (which std::vector is basically a wrapper for). Array indices start at 0 because it makes the math easier in the long run. Size is the size of the array, not the index of the last element, because that's not the size. Having indices run from 0 to (size-1) is really, really useful. Trust me, as an experienced programmer (and trust every other experienced programmer around here).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!