It works! No more C++
[hammerdown.git] / SDLTest / main.cpp
blobb213fa511e734b8d7dff9084848ebcd1896d8093
2 #include "main.h"
5 int main (int argc, char *argv[]) {
7 //Inicializa SDL
8 if (SDL_Init (SDL_INIT_VIDEO) < 0) {
9 printf ("SDL_INIT ha fallado\n");
10 exit (1);
13 atexit (SDL_Quit);
15 //Usamos HW si disponible, double buffering, y pantalla completa
16 pantalla = SDL_SetVideoMode ( 800, 600, 16, SDL_SWSURFACE | SDL_DOUBLEBUF );
18 if (pantalla == NULL) {
19 printf ("Modo de video erroneo\n");
20 exit (1);
23 //Usaremos nuestro propio cursor
24 SDL_ShowCursor (SDL_DISABLE);
26 if (!iniciar()) {
27 printf ("Error de inicializacion\n");
28 exit (1);
32 //Bucle principal
34 while (true) {
37 //Actualizamos la pos. de la camara a partir del cursor
38 if (cursorX < 8)
39 scrollX -= (8 - cursorX);
40 if (cursorX > 792)
41 scrollX += (cursorX - 792);
43 if (cursorY < 8)
44 scrollY-= (8 - cursorY);
45 if (cursorY > 592)
46 scrollY += (cursorY - 592);
48 //Dibujamos
49 dibujar ();
52 //Control de eventos SDL
54 SDL_Event ev;
56 while (SDL_PollEvent (&ev)) {
58 switch (ev.type) {
59 //Pulsacion del teclado
60 case SDL_KEYDOWN:
61 if (ev.key.keysym.sym == SDLK_ESCAPE) {
62 limpiar();
63 return 0;
65 break;
67 //Movimiento del raton
68 case SDL_MOUSEMOTION:
69 cursorX = ev.motion.x;
70 cursorY = ev.motion.y;
71 break;
73 //Fin del programa
74 case SDL_QUIT:
75 limpiar();
76 return 0;
77 break;
85 return 0;
89 //Inicializa lo que haga falta y carga las superficies
90 bool iniciar () {
91 SDL_Surface *temp;
93 /* Muestra alguna informacion sobre el sistema */
95 const SDL_VideoInfo *vi;
96 vi = SDL_GetVideoInfo ();
98 printf ("hw_available: %d\nblit_hw: %d\nblit_hw_CC: %d\nblit_hw_A: %d\nblit_sw: %d\nblit_sw_CC: %d\n"
99 "blit_SW_A: %d\nVideo_mem: %d Kb\n",
100 vi->hw_available,
101 vi->blit_hw,
102 vi->blit_hw_CC,
103 vi->blit_hw_A,
104 vi->blit_sw,
105 vi->blit_sw_CC,
106 vi->blit_sw_A,
107 vi->video_mem);
109 /**/
111 //Definimos el clipper
112 SDL_SetClipRect (pantalla, NULL);
114 //Cargamos los tiles
115 temp = SDL_LoadBMP ("tiles.bmp");
117 if (temp == NULL)
118 return false;
120 tiles = SDL_CreateRGBSurface (SDL_HWSURFACE, temp->w, temp->h, vi->vfmt->BitsPerPixel,
121 vi->vfmt->Rmask, vi->vfmt->Gmask, vi->vfmt->Bmask,
122 vi->vfmt->Amask);
125 SDL_SetColorKey (tiles, SDL_SRCCOLORKEY, 0);
127 SDL_BlitSurface (temp, NULL, tiles, NULL);
129 SDL_FreeSurface (temp);
132 //Calculamos el total de tiles
133 nTilesX = tiles->w / TILE_ANCHO;
134 nTilesY = tiles->h / TILE_ALTO;
137 //Cargamos la flecha para el cursor
138 temp = SDL_LoadBMP ("cursor.bmp");
140 if (temp == NULL)
141 return false;
143 flecha = SDL_CreateRGBSurface (SDL_SWSURFACE, temp->w, temp->h, vi->vfmt->BitsPerPixel,
144 vi->vfmt->Rmask, vi->vfmt->Gmask, vi->vfmt->Bmask,
145 vi->vfmt->Amask);
148 SDL_SetColorKey (flecha, SDL_SRCCOLORKEY, 0);
150 SDL_BlitSurface (temp, NULL, flecha, NULL);
152 SDL_FreeSurface (temp);
155 //Muestra informacion indicando en que memoria estan las superficies
157 if ((tiles->flags & SDL_HWSURFACE) == 1)
158 printf ("TILES en HW\n");
159 else
160 printf ("TILES en SW\n");
162 if ((flecha->flags & SDL_HWSURFACE) == 1)
163 printf ("FLECHA en HW\n");
164 else
165 printf ("FLECHA en SW\n");
168 return true;
172 //Limpia la memoria
173 void limpiar () {
175 printf ("Limpiando...\n");
177 if (tiles != NULL) {
178 SDL_FreeSurface (tiles);
179 tiles = NULL;
181 if (flecha != NULL) {
182 SDL_FreeSurface (flecha);
183 flecha = NULL;
189 //Dibuja el mapa y el cursor
190 void dibujar () {
192 //Limpia la pantalla
193 SDL_FillRect (pantalla, NULL, 0);
196 dibujarMapa();
197 dibujarCursor (cursorX, cursorY);
199 //Intercambia los buffers
200 SDL_Flip (pantalla);
205 //Dibuja los tiles del mapa
206 void dibujarMapa () {
208 int x, y; //Casilla del mapa
209 int px, py; //Coordenadas en pantalla
212 //Por cada casilla del mapa
213 for (y = 0; y < MAPA_ALTO; y++) {
214 for (x = 0; x < MAPA_ANCHO; x++) {
216 //Calculamos las coordenadas de pantalla a partir de las de la casilla
217 //y el desplazamiento de la camara (scrollX, scrollY)
218 px = (x - y)*(TILE_ANCHO/2) - scrollX;
219 py = (x + y)*(TILE_ALTO/2) - scrollY;
221 //Dibuja el tile en la pantalla
222 blitTile (pantalla, px, py, mapa[x][y]);
229 //Dibuja la flecha del cursor
230 void dibujarCursor (int x, int y) {
232 SDL_Rect dRect;
233 dRect.x = x; dRect.y = y;
234 dRect.w = dRect.h = 16;
236 SDL_BlitSurface (flecha, NULL, pantalla, &dRect);
241 //Dibuja un tile en la posicion (x, y)
242 void blitTile (SDL_Surface *dest, int x, int y, int nTile) {
244 if (nTile >= nTilesX * nTilesY) return;
247 SDL_Rect srcRect, destRect;
248 destRect.x = x; destRect.y = y;
250 srcRect.x = (nTile % nTilesX) * TILE_ANCHO;
251 srcRect.y = (nTile / nTilesX) * TILE_ALTO;
252 srcRect.w = TILE_ANCHO; srcRect.h = TILE_ALTO;
254 SDL_BlitSurface (tiles, &srcRect, dest, &destRect);