Page 1 of 1

Can Anyone Help Me Out?

Posted: Sun May 16, 2010 5:41 pm
by Lord Pingas
Sorry, it's late at night and I want to get this working before I get to sleep.

Code: Select all

#include "SDL.h"
#include <string>

const int Grass = 0;
const int Wall = 1;

int xPos = 200;
int yPos = 300;

int map[12][16] =
{
	{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
	{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
	{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
	{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}
};

SDL_Surface *load_image(std::string filename)
{
    SDL_Surface *loadedImage = NULL;

	SDL_Surface *optimizedImage = NULL;

	loadedImage = SDL_LoadBMP(filename.c_str());

	if(loadedImage != NULL)
	{
		optimizedImage = SDL_DisplayFormat(loadedImage);

		SDL_FreeSurface(loadedImage);
	}
	return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, NULL, destination, &offset);
}

int main(int argc, char *args[])
{
    SDL_Event event;

    bool quit = false;

    SDL_Surface *screen;
    SDL_Surface *player;
    SDL_Surface *wall;
    SDL_Surface *grass;

	player = load_image("gfx/player.bmp");
	wall = load_image("gfx/wall.bmp");
	grass = load_image("gfx/grass.bmp");

	SDL_Init(SDL_INIT_EVERYTHING);

    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

	SDL_WM_SetCaption("My RPG", NULL);

	while(quit == false)
	{
		while(SDL_PollEvent(&event))
		{
			for(int y = 0; y < 12; y++)
			{
				for(int x = 0; x < 16; x++)
				{
					if(map[x][y] == Wall)
					{
						apply_surface(x*40, y*40, wall, screen);
					}
					if(map[x][y] == Grass)
					{
						apply_surface(x*40, y*40, grass, screen);
					}
				}
			}

			switch(event.key.keysym.sym)
			{
			case SDLK_UP: yPos--; break;
			case SDLK_DOWN: yPos++; break;
			case SDLK_LEFT: xPos--; break;
			case SDLK_RIGHT: xPos++; break;
			}

			apply_surface(xPos, yPos, player, screen);
		    
			if(event.type == SDL_QUIT)
		    {
			    quit = true;
		    }

			SDL_Flip(screen);
		}
	}
	
	SDL_FreeSurface(player);
	SDL_FreeSurface(wall);
	SDL_FreeSurface(grass);

	SDL_Quit();

	return 0;
}
Sorry for the whole bunch of code and tell me off because I know the whole "no heaps of code" policy but this was my first time trying to work with tiling in a program and I'm desperate.

The problem is that when I compile the project I get a send error report and I have to close it.

Any ideas why that would happen?

PS: I know this code is ugly as all living and breathing f*ck but I was speeding through this code and I will touch up on it later...

PSS: Oh and I've not seen the result of this program yet because it closes before I get a chance to see it.

Re: Can Anyone Help Me Out?

Posted: Sun May 16, 2010 6:22 pm
by Live-Dimension
I can't see anything hugely wrong with the code, except your doing a lot of drawing in the event loop. Minus using up alot of cpu cycles I don't think this would cause any issues, but whatever, could be the cause.

Shouldn't the DrawTiles function be outisde the event? As well, shouldn't the player be drawn out of it? Everytime there are multiple events your doing ti over and over :P

I'll just substitute the draw calls for obvious function calls.

Code: Select all

while(quit == false) {
      DrawTiles();
      while(SDL_PollEvent(&event)) {
         //process events
       }
      DrawPlayer();
}

Re: Can Anyone Help Me Out?

Posted: Sun May 16, 2010 6:35 pm
by Lord Pingas
It's okay I fixed it...

I should of called SDL_SetVideoMode() before loading the surface files.
Live-Dimension wrote:I can't see anything hugely wrong with the code, except your doing a lot of drawing in the event loop. Minus using up alot of cpu cycles I don't think this would cause any issues, but whatever, could be the cause.

Shouldn't the DrawTiles function be outisde the event? As well, shouldn't the player be drawn out of it? Everytime there are multiple events your doing ti over and over :P

I'll just substitute the draw calls for obvious function calls.

Code: Select all

while(quit == false) {
DrawTiles();
while(SDL_PollEvent(&event)) {
//process events
}
DrawPlayer();
}
Oh, I still got a lot to learn. :lol:

I will start this code over again next morning and this time I will structure it.

Thanks Live-Dimension. :)

Re: Can Anyone Help Me Out?

Posted: Mon May 17, 2010 11:14 am
by dandymcgee
Lord Pingas wrote: I should of called SDL_SetVideoMode() before loading the surface files.
This is a very valuable piece of information when working with SDL.

As a rule of thumb SDL_SetVideoMode() should be one of the first functions called, if not the THE first function called, in any SDL program. Doing so will save you much debugging grief. ;)

Re: Can Anyone Help Me Out?

Posted: Mon May 17, 2010 4:10 pm
by eatcomics
And another thing to remember

Make sure you include SDL.h before any of your local headers :P I had trouble with that the other day, took me a quite a while to figure out what I did wrong