Can Anyone Help Me Out?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Lord Pingas
Chaos Rift Regular
Chaos Rift Regular
Posts: 178
Joined: Thu Dec 31, 2009 9:33 am
Favorite Gaming Platforms: NES, SNES, Nintendo 64, Dreamcast, Wii
Programming Language of Choice: C++
Location: Hiding In My Mum's Basement With My Pokemon Cards

Can Anyone Help Me Out?

Post 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.
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: Can Anyone Help Me Out?

Post 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();
}
Image
User avatar
Lord Pingas
Chaos Rift Regular
Chaos Rift Regular
Posts: 178
Joined: Thu Dec 31, 2009 9:33 am
Favorite Gaming Platforms: NES, SNES, Nintendo 64, Dreamcast, Wii
Programming Language of Choice: C++
Location: Hiding In My Mum's Basement With My Pokemon Cards

Re: Can Anyone Help Me Out?

Post 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. :)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Can Anyone Help Me Out?

Post 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. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Can Anyone Help Me Out?

Post 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
Image
Post Reply