2012-05-03 104 views
1

我遇到一个特定的问题,而我在我的文件上使用SDL_MapRGB jeu.c我试图做一个视频游戏,因为我学习C,我想改善我的通过使用结构的代码:SDL_MapRGB崩溃,没有错误信息

的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 

和来到这里的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)); 

这条线开始SDL_FillRect我不知道为什么,但不管我正在写的程序崩溃之后。我用了很多的printf找到这个问题.. 我也试试这个:

SDL_Surface * ecran; 
Uint32 couleur=SDL_MapRGB(jeu->ecran->format, 0, 0, 0); 
SDL_FillRect(jeu->ecran, NULL, couleur); 

和程序崩溃SDL_FILLRect

我知道这是很难读小白代码,但我花了一我的这一天没有解决方案。 感谢您阅读

回答

1

你似乎对未初始化的指针被调用SDL_FillRect()SDL_Surface - 要么是NULL或无效,你的代码崩溃的时候了(幸运的情况下),或者恰好指向有效的存储位置(和你最终会破坏你的程序内存)。

取决于你打算以后怎么使用它,无论是在堆中分配一些内存为您的表面

SDL_Surface *ecran = malloc(sizeof(SDL_Surface)); 

或者干脆把整个事情在堆栈上,并通过它的地址SDL_FillRect(也许是更好选项,如果你不打算在jouer功能以外使用它

SDL_Surface ecran; 
SDL_FillRect(&ecran, ...)