Original Post
Hi all, I am am starting to program a 2D RPG in SDL, and I just wanted to ask everyone to critic the way I want to do this. at the moment I have 4 files, main.cpp, main.h, world.cpp and world.h(nothing in it atm). Here they are: main.cpp main.h world.cpp It does nothing more then display 80*60 squares on the screen. I was going to make the hero move by squares(using the MAP struct's x and y value), and colission detection would be done with the int value inside my WORLD struct. Anyone see something I could optimize already?
#include <SDL/SDL.h>
#include <iostream>
#include "main.h"
using namespace std;
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
void InitSDL()
{
if(SDL_Init(SDL_INIT_AUDIO) < 0)
cout << "Error Initiating SDL...";
atexit(SDL_Quit);
}
int main(int argc, char *argv[])
{
InitSDL();
if(!InitWorld())
cout << "Error Initiating the world...";
DisplayWorld(screen);
bool running = true;
while(running)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_KEYDOWN)
{
running = false;
}
}
}
return 0;
}
#ifndef MAIN_H
#define MAIN_H
bool InitWorld();
void DisplayWorld(SDL_Surface*);
#endif
#include "world.h"
#include <SDL/SDL_image.h>
struct WORLD
{
SDL_Surface *image;
int value;
int x, y;
};
bool InitWorld()
{
WORLD world[40][30];
SDL_Surface *temp = IMG_Load("map.gif");
for(int i = 0; i < 40; i++)
{
for(int j = 0; j < 30; j++)
{
world[j].image = temp;
world[j].value = 0;
world[j].x = i * 20;
world[j].y = j * 20;
}
}
return true;
}
void DisplayWorld(SDL_Surface *screen)
{
WORLD world[40][30];
for(int i = 0; i < 40; i++)
{
for(int j = 0; j < 30; j++)
{
SDL_Rect rect;
rect.x = world[j].x;
rect.y = world[j].y;
SDL_BlitSurface(world[j].image, NULL, screen, ▭);
}
}
}