Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C SDL_MapRGB正在崩溃,没有错误消息_C_Sdl - Fatal编程技术网

C SDL_MapRGB正在崩溃,没有错误消息

C SDL_MapRGB正在崩溃,没有错误消息,c,sdl,C,Sdl,当我在我的文件jeu.c上使用SDL_MapRGB时,我遇到了一个特定的问题。我试图制作一个视频游戏,因为我在学习c,我想通过使用一种结构来改进我的代码: main.c #include <stdlib.h> #include <stdio.h> #include <SDL/SDL.h> #include <SDL_image.h> #include "constantes.h" #include "jeu.h" #include "editeur

当我在我的文件jeu.c上使用SDL_MapRGB时,我遇到了一个特定的问题。我试图制作一个视频游戏,因为我在学习c,我想通过使用一种结构来改进我的代码:

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;
}
#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之前崩溃了

我知道这很难读懂noob代码,但我整天都在这个问题上寻找解决方案。
感谢您阅读

您似乎在调用一个指向
SDL_Surface
的未初始化指针上的
SDL_FillRect()
——该指针为
NULL
或无效,您的代码立即崩溃(幸运的情况)或恰好指向一个有效的内存位置(最终会损坏您的程序内存)

根据您以后打算如何使用它,可以为堆上的曲面分配一些内存

SDL_Surface *ecran = malloc(sizeof(SDL_Surface));
或者将整个内容放在堆栈上,并将其地址传递给SDL_FillRect(如果您不打算在
jouer
函数之外使用它,可能是更好的选择)

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

您似乎正在对指向
SDL\u Surface
的未初始化指针调用
SDL\u FillRect()
——该指针为
NULL
或无效,并且您的代码立即崩溃(幸运的情况)或恰好指向有效的内存位置(最终会损坏程序内存)

根据您以后打算如何使用它,可以为堆上的曲面分配一些内存

SDL_Surface *ecran = malloc(sizeof(SDL_Surface));
或者将整个内容放在堆栈上,并将其地址传递给SDL_FillRect(如果您不打算在
jouer
函数之外使用它,可能是更好的选择)

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