Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:23:06+00:00 2026-06-03T02:23:06+00:00

I encounter a specific problem while i’m using SDL_MapRGB on my file jeu.c i’m

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:23:07+00:00Added an answer on June 3, 2026 at 2:23 am

    You seem to be calling SDL_FillRect() on an uninitialized pointer to SDL_Surface – which is either NULL or 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

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

    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 jouer function

    SDL_Surface ecran;
    SDL_FillRect(&ecran, ...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to working with XML, and I've encountered a weird problem while trying
I have encounter a problem using gdb to debug vlc.exe in windows environment. I
I encounter this issue pretty consistently when trying to merge a branch back into
i encounter an error like Error 1 fatal error LNK1107: invalid or corrupt file:
I just encounter a strange problem: var a:ClassA = new ClassA; var b:ClassA =
It seems that all questions regarding this topic are very specific, and while I
I've been using a system with imagettftext for a while now to add nog
I have encountered an unexpected Access Error while running a project I've built using
Bear with me, this is possibly a very specific problem we've encountered here: We
I'm having a hard time trying to make jquery.form with a cross-domain request. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.