Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Ferno en 4 Diciembre 2011, 03:20 am



Título: Duda SDL_init()
Publicado por: Ferno en 4 Diciembre 2011, 03:20 am
Gente, estoy con algo bastante curioso (o tan tonto que no lo veo :P).
Resulta que estoy trabajando con la librería SDL, en Eclipse. Tengo 2 proyectos diferentes, con un archivo source cada uno. Aclaro que ambos proyectos están en el mismo directorio y tienen exactamente las mismas propiedades.

Ahora bien, en un source tengo este código:


Código
  1. #include <stdio.h>
  2. #include <SDL/SDL.h>
  3.  
  4. int main (int argc, char* argv[])
  5. {
  6. SDL_Surface* screen;
  7. SDL_Surface* surface;
  8. Uint32 rmask = 0xff000000;
  9. Uint32 gmask = 0x00ff0000;
  10. Uint32 bmask = 0x0000ff00;
  11. Uint32 amask = 0x000000ff;
  12.  
  13. if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0)
  14. {
  15. printf("No se puede inicializar SDL: %s\n",SDL_GetError());
  16. exit(1);
  17. }
  18.  
  19. screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE | SDL_DOUBLEBUF);
  20. if ((screen == NULL))
  21. {
  22. fprintf(stderr, "No se puede establecer el modo video 640x480: %s \n",SDL_GetError());
  23. exit(1);
  24. }
  25.  
  26. surface = SDL_CreateRGBSurface(SDL_SWSURFACE,640,480,32,rmask,gmask,bmask,amask);
  27. if ((surface == NULL))
  28. {
  29. fprintf(stderr,"Error al crear pantalla %s \n",SDL_GetError());
  30. exit(1);
  31. }
  32.  
  33.  
  34.  
  35. printf("Está todo más que bien\n");
  36. atexit(SDL_Quit);
  37. return 0;
  38. }
  39.  

Algo muy simple en SDL, recién para arrancar. Compila, lo corro, y funciona de pelos.
Ahora bien en el otro source tengo este otro código:


Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <SDL/SDL.h>
  4.  
  5. int main (int argc, char* argv[])
  6. {
  7. SDL_Surface *screen, *image;
  8. Uint32 rmask, bmask, gmask, amask;
  9. SDL_Rect dest; /*Destiny of the image*/
  10. SDL_Event event;
  11. int done = 0;
  12.  
  13. atexit(SDL_Quit);
  14.  
  15. /*INIT SDL*/
  16. if (SDL_Init(SDL_INIT_VIDEO) < 0);
  17. {
  18. printf("No se pudo inicializar SDL %s \n", SDL_GetError());
  19. exit(1);
  20. }
  21.  
  22. /*ACTIVATE VIDEO MODE*/
  23. screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE);
  24. if ((screen == NULL))
  25. {
  26. printf("No se pude inicializar el modo gráfico %s \n",SDL_GetError());
  27. exit(1);
  28. }
  29.  
  30. /*UPLOAD GRAPHIC*/
  31. image = SDL_LoadBMP("Ruta.bmp");
  32. if (image == NULL)
  33. {
  34. printf("No se pudo cargar gráfico %s \n",SDL_GetError());
  35. exit(1);
  36. }
  37.  
  38. /*Defining destiny of the image*/
  39. dest.x = 100;
  40. dest.y = 100;
  41. dest.w = image->w;
  42. dest.h = image->w;
  43. SDL_BlitSurface(image,NULL,screen,&dest);
  44.  
  45. /*Show Screen*/
  46. SDL_Flip(screen);
  47.  
  48. /*Free Surface*/
  49. SDL_FreeSurface(image);
  50.  
  51. /*Waiting for the user to press to quit*/
  52. while (done == 0)
  53. {
  54. while (SDL_PollEvent(&event))
  55. {
  56. if (event.type == SDL_KEYDOWN)
  57. done = 1;
  58. }
  59. }
  60.  
  61. /*FINISH SDL*/
  62. return 0;
  63. }
  64.  

También compila perfecto. El problema es que al correrlo, me da un error en el SDL_Init. Es decir, entra al if del SDL_Init.
Lo curioso es que limpio esa parte del código, es decir, no inicializo el SDL, y funciona perfectamente el programa. Carga la ventana con la respectiva imágen.

No entiendo, ¿Alguien sabe qué está ocurriendo?


Título: Re: Duda SDL_init()
Publicado por: Ferno en 7 Diciembre 2011, 19:31 pm
Pfff, soy lo PEOR!!
Error: Si se fijan tengo un ";" luego del if del SDL_Init del segundo código.

Estos errores de tipeo me molestan demasiado :P


Título: Re: Duda SDL_init()
Publicado por: Littlehorse en 9 Diciembre 2011, 01:04 am
Pfff, soy lo PEOR!!
Error: Si se fijan tengo un ";" luego del if del SDL_Init del segundo código.

Estos errores de tipeo me molestan demasiado :P

Cosas que pasan en el fragor de la batalla contra el código  ;D