okay, I’ll post some code from my current project using SDL 1.3:
Code:
int SDL_main(int argc, char* argv[])
{
printf(“Starting SDL\n”);
if(SDL_Init(SDL_INIT_TIMER|SDL_INIT_AUDIO|SDL_INIT_VIDEO) == -1){
printf(“Failed to start SDL! Reason: %s\n”, SDL_GetError());
return 1;
}
printf("Creating a Window\n");
// Create an SDL Window -- fullscreen and shown
// NOTE: try to find the actual screen values, this will probably fail for iPad
SDL_Window* window = SDL_CreateWindow("SDL Window", 20, 20, 480, 320, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_SHOWN);
printf("Initializing Rendering Backend\n");
// Initialize the Rendering Backend: hardware accelerated with a backbuffer
// TODO: Make sure we choose GLES?
int index = -1;
// See what renderers SDL has available
for(index = 0; index < SDL_GetNumRenderDrivers(); index++)
{
printf("Getting info on driver %d\n", index);
SDL_RendererInfo info;
if(SDL_GetRenderDriverInfo(index, &info) != -1){
printf("Driver %s index: %d\n\n", info.name, index);
}
}
// Letting SDL choose... single backbuffer and hardware accelerated
if(SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_ACCELERATED) == -1){
// D3D/GL/GLES failed, so let's just use software
printf("Failed to get accelerated backend\n");
if(SDL_CreateRenderer(window, -1, 0) == -1){
// No rendering is available?
printf("SDL has no rendering backend!\n");
printf("Destroying Window and Shutting Down SDL\n");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
}
SDL_RendererInfo info;
if(SDL_GetRendererInfo(&info) == -1){
printf("Error: Could not get rendering info\n");
}
else {
printf("SDL chose %s Rendering Backend\n", info.name);
}
printf("Starting Event Loop\n");
SDL_Event event;
GameMode* running = new TitleScreen();
while(running)
{
if(SDL_PollEvent(&event) == 1){
if(event.type == SDL_QUIT){
printf("Quitting\n");
running = false;
break;
}
// Pressing the "X" button in the window -- this used to be type SDL_QUIT as above
else if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE){
// Q: Should I place a Quit event in queue instead?
printf("Quitting\n");
running = false;
break;
}
running->eventLogic(&event);
}
// Fill the screen with black.
SDL_RenderClear();
running->renderLogic();
// Update the screen
SDL_RenderPresent();
running = running->gameLogic();
// Delaying just 1 millisecond will prevent the event loop from trying to hog resources and waste battery power.
SDL_Delay(1);
}
// Cleaning up SDL
printf("Destroying Window and Shutting Down SDL\n");
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I use two classes here called “GameMode” and “TitleScreen”, which for understanding purposes I’ll talk about (in case they confuse you)
TitleScreen is an implementation of GameMode, GameMode is essentially just an interface (a class with no members or function implementations).
The function GameMode::eventLogic(SDL_Event*) handles user input.
The function GameMode::renderLogic(void) renders the game area.
The function GameMode::gameLogic(void) performs any auxiliary game functions – AI, physics, whatever.
I’ve been using this design for single-threaded games for about 4 years now, it’s very simple and straightforward and works quite well.
Aside for those, hopefully the comments will be helpful with explaining the use of the functions there and why I use flags I used etc.
I could also post my code for loading and rendering images, but it’s pretty much garbage right now. It needs more playing with. The presense of the API documentation in the headers should be enough to lead you on the right path. Also, look at my topic “Am I doing something wrong?” to see how I worked around problems I came across with the new API.------------------------
EM3 Nathaniel Fries, U.S. Navy
http://natefries.net/