NOGDUS $1670.00 has been donated to NOGDUS!
June 29, 2017, 05:50:36 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: SDL Quick Start main.cpp  (Read 4000 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: April 27, 2009, 10:44:20 AM »

SDL Quick Start main.cpp

Below is some code to let you jump right into working with SDL.
Its got frame-capped timing at roughly 30 FPS, and a nice and easy input handler. Smiley

Code:
// quickstart_main.cpp
// Project: SDL Quick Start Example
// Author: Richard Marks

// C STANDARD LIBRARY
#include <cstdio>
#include <cstdlib>

// SDL
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#define SCREEN_W 1024
#define SCREEN_H 768
#define SCREEN_BPP 24

// a useful keyboard handler structure to simplify SDL keyboard control
struct Keyboard
{
// this holds the number of keys
int keycount;

// this holds the state of every key
unsigned char* key;

// this is used to create non-repeating key presses
bool* keywaspressed;
};

// creates a new instance of the keyboard handler structure
// make sure you call this AFTER you init SDL and only call it once
Keyboard* keyboard_create()
{
Keyboard* keyboard = new Keyboard;

keyboard->key = SDL_GetKeyState(&keyboard->keycount);

keyboard->keywaspressed = new bool [keyboard->keycount];

for (int index = 0; index < keyboard->keycount; index++)
{
keyboard->keywaspressed[index] = false;
}

return keyboard;
}

// updates the keyboard structure for us
void keyboard_update(Keyboard* keyboard)
{
// bail out the function if the pointer is invalid
if (!keyboard)
{
return;
}

SDL_PumpEvents();
keyboard->key = SDL_GetKeyState(0);
}

// destroys the keyboard handler structure
void keyboard_destroy(Keyboard* keyboard)
{
// bail out the function if the pointer is invalid
if (!keyboard)
{
return;
}

// clear our tracking array
if (keyboard->keywaspressed)
{
delete [] keyboard->keywaspressed;
}

// free the pointer
delete keyboard;

// init to zero to prevent logic errors
keyboard = 0;
}

int main(int argc, char* argv[])
{
// init SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
fprintf(stderr, "SDL Library Initialization Failed!\n\tSDL Error: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

// create the window
if (!SDL_SetVideoMode(SCREEN_W, SCREEN_H, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF))
{
fprintf(stderr, "SDL Screen Initialization Failed! %dx%d@%dbpp\nSDL Error: %s\n",
SCREEN_W,
SCREEN_H,
SCREEN_BPP,
SDL_GetError());
exit(1);
}

// set the window caption
SDL_WM_SetCaption("SDL Quick Start Example -- by Richard Marks <ccpsceo@gmail.com>", 0);

// create our keyboard handler
Keyboard* keyboard = keyboard_create();

fprintf(stderr, "\nstarting main loop...\n\n");
// start the main loop
SDL_Event event;
bool running = true;
int startticks = SDL_GetTicks();
int mainlooptimer = SDL_GetTicks();
while(running)
{
// check for our window being closed
while(SDL_PollEvent(&event))
{
if (SDL_QUIT == event.type)
{
running = false;
}
}

// update our keyboard handler
keyboard_update(keyboard);

// start timing our code
startticks = SDL_GetTicks();

// check for our controls

// ESC and Q quits
if (keyboard->key[SDLK_ESCAPE] || keyboard->key[SDLK_q])
{
running = false;
}

// render our scene
// draw here!

// flip our double buffer
SDL_Flip(SDL_GetVideoSurface());

// lock our framerate to roughly 30 FPS
int tickdiff = SDL_GetTicks() - startticks;
if (tickdiff < 33)
{
SDL_Delay(33 - tickdiff);
}
}

int tickdiff = SDL_GetTicks() - mainlooptimer;

fprintf(stderr, "finished after %d ticks!\n\n", tickdiff);

// destroy our pointers

keyboard_destroy(keyboard);

return 0;
}


Let me know if you have any questions.
Logged

oenone
Offline Offline

Respect: 10
« Reply #1 on: August 27, 2009, 07:27:15 AM »

Hi,

I tested this code with Sun Studio Express (on OpenSolaris 2009.06 b118), using Sun's compilers. It worked, but there is no package for SDLimage, so I had to remove that include. It's not used anyway.

I'll play a bit more with Sun Studio, and will post some helpful stuff I discover Smiley

Greetings,
oenone
Logged

If you don't remember something, it never existed...
If you aren't remembered, you never existed...
I don't quite understand what love is like... But if there was someone who liked me, I'd be happy.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« Reply #2 on: August 27, 2009, 08:08:42 AM »

You have you compile the library from source if there is no package readily available.

http://www.libsdl.org/projects/SDL_image/
Logged

Tags: SDL code  SDL quickstart  main.cpp 
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!