I encounter a specific problem while i’m using SDL_MapRGB on my file jeu.c i’m trying to make a videogame and because i’m learning C, I wanted to improve my code by using a structure:
main.c
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL_image.h>
#include "constantes.h"
#include "jeu.h"
#include "editeur.h"
int main(int argc, char *argv[])
{
Partie *jeu = NULL;
jeu = (Partie *)malloc(sizeof(Partie));
if (jeu ==NULL){
fprintf(stderr,"Problème d'allocation de mémoire");
return 1;
}
SDL_Surface *ecran = NULL, *menu = NULL;
SDL_Rect positionMenu;
SDL_Event event;
int continuer = 1;
SDL_Init(SDL_INIT_VIDEO);
if(SDL_Init(SDL_INIT_VIDEO)!=0)
{
fprintf(stderr,"probleme d'init video: %s\n", SDL_GetError ());
};
SDL_WM_SetIcon(IMG_Load("img/icone.png"), NULL); // L'icône doit être chargée avant SDL_SetVideoMode
ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
printf("Mode vidéo: dx%d\n", ecran->w, ecran->h, ecran->format->BitsPerPixel);
SDL_WM_SetCaption("pacman", NULL);
menu = IMG_Load("img/menu.png");
positionMenu.x = 0;
positionMenu.y = 0;
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
continuer = 0;
break;
case SDLK_RETURN: //
jouer (ecran);
break;
case SDLK_SPACE: // Demande à jouer
jouer (ecran);
break;
case SDLK_KP_ENTER: // Demande à jouer
jouer (ecran);
break;
case SDLK_KP1: // Demande à jouer
jouer (ecran);
break;
case SDLK_KP2: // Demande l'éditeur de niveaux
editeur(ecran);
break;
}
break;
}
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
SDL_BlitSurface(menu, NULL, ecran, &positionMenu);
SDL_Flip(ecran);
}
SDL_FreeSurface(menu);
SDL_Quit();
return EXIT_SUCCESS;
}
constantes.h
#ifndef DEF_CONSTANTES
#define DEF_CONSTANTES
#include <SDL/SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#define TAILLE_BLOC 25
#define NB_BLOCS_LARGEUR 28
#define NB_BLOCS_HAUTEUR 28
#define LARGEUR_FENETRE TAILLE_BLOC * NB_BLOCS_LARGEUR
#define HAUTEUR_FENETRE TAILLE_BLOC * NB_BLOCS_HAUTEUR
enum {HAUT, BAS, GAUCHE, DROITE};
enum {VIDE, MUR, GRAINE, ENNEMI, BONUS, PACMAN};
struct partie
{
SDL_Surface * ecran;
int score;
};
typedef struct partie Partie;
#endif
and here come the jeu.c
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include "constantes.h"
#include "jeu.h"
int jouer(Partie * jeu)
{
SDL_Surface * ecran;
SDL_FillRect(ecran, NULL, SDL_MapRGB(jeu->ecran->format, 0, 0, 0));
after this line starting with SDL_FillRect i dont know why but whatever i’m writing the program is crashing. I locate this problem by using a lot of printf..
I also try this instead :
SDL_Surface * ecran;
Uint32 couleur=SDL_MapRGB(jeu->ecran->format, 0, 0, 0);
SDL_FillRect(jeu->ecran, NULL, couleur);
and the program crashed before SDL_FILLRect
I know this is hard to read noob code, but I spent all my day on this problem for no solutions.
Thank you for reading
You seem to be calling
SDL_FillRect()on an uninitialized pointer toSDL_Surface– which is eitherNULLor invalid and your code crashes right away (the lucky case) or happens to point to a valid memory location (and you end up corrupting your programs memory).Depending on how you plan to use it later, either allocate some memory for your surface on the heap
or just put the whole thing on the stack and pass it’s address to SDL_FillRect (maybe the better option if you don’t plan to use it outside the
jouerfunction