/[projet1]/users/miri/mirisdl/mirisdl.c
Defence Force logotype

Contents of /users/miri/mirisdl/mirisdl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 100 - (show annotations)
Tue Jun 16 16:46:54 2009 UTC (10 years, 11 months ago) by miri
File MIME type: text/plain
File size: 2116 byte(s)
First version of Hello World - MiriSDL
1 #include "SDL/SDL.h"
2 #include <string>
3
4
5 const int SCREEN_WIDTH = 640;
6 const int SCREEN_HEIGHT = 480;
7 const int SCREEN_BPP = 32;
8
9 SDL_Surface *message = NULL;
10 SDL_Surface *background = NULL;
11 SDL_Surface *screen = NULL;
12
13 SDL_Surface *load_image( std::string filename)
14 {
15 //Temporary storage for the image that's loaded.
16 SDL_Surface* loadedImage = NULL;
17 //The optimized image that wil be used.
18 SDL_Surface* optimizedImage = NULL;
19 //Load the image
20 loadedImage = SDL_LoadBMP( filename.c_str());
21 //If nothing went wrong loading the image
22 if (loadedImage != NULL)
23 {
24 //Create an optimized image
25 optimizedImage = SDL_DisplayFormat(loadedImage);
26 //Free the old image
27 SDL_FreeSurface(loadedImage);
28 }
29 //return the optimized image
30 return optimizedImage;
31 }
32
33 void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
34 {
35 //Make a temporary rectangle to hold the offsets
36 SDL_Rect offset;
37 //Give the offsets to the rectangle
38 offset.x = x;
39 offset.y = y;
40 //Blit the surface
41 SDL_BlitSurface(source, NULL, destination, &offset);
42 }
43
44
45 int main( int argc, char* args[] )
46 {
47 //Initialize SDL subsystem
48 if( SDL_Init (SDL_INIT_EVERYTHING) == -1)
49 {
50 return 1;
51 }
52 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
53 if (screen == NULL)
54 {
55 return 1;
56 }
57 //Set the window caption
58 SDL_WM_SetCaption("Hello World!", NULL);
59 //Load the images
60 message=load_image("hello_world.bmp");
61 background=load_image("background.bmp");
62 //Apply the background to the screen
63 apply_surface(0,0,background,screen);
64 //Apply the message to the screen
65 apply_surface(180,140,message, screen);
66 //Update the screen
67 if(SDL_Flip(screen) == -1)
68 {
69 return 1;
70 }
71 //Wait 2 sec
72 SDL_Delay(2000);
73 //Free the surfaces
74 SDL_FreeSurface(message);
75 SDL_FreeSurface(background);
76 //Quit SDL
77 SDL_QUIT();
78
79 return 0;
80
81 }

  ViewVC Help
Powered by ViewVC 1.1.26