Freaky SDL conundrum

This is a discussion on Freaky SDL conundrum within the Game Programming forums, part of the General Programming Boards category; Hi, I've been working on an interactive titlescreen which is slowly becoming a simple-ish graphical RPG, and I've been solving ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Freaky SDL conundrum

    Hi, I've been working on an interactive titlescreen which is slowly becoming a simple-ish graphical RPG, and I've been solving all my other issues well enough, but... For some reason, the image of the titlescreen doesn't appear until the user hits the up or down arrow key and I can't identify the problem at all. I'm completely at a loss. This is my first project this complex, and I'm just about begging: Can someone find the flaw?? If someone can point out the problem, I'll be eternally grateful! Here's my code, which still has my terminal/debugging messages:

    Code:
    #include "SDL/SDL.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    SDL_Surface *screen;
    SDL_Event event;
    int menuChoice = 0;
    int title(), ttlMenu(), newGame(), loadGame();
    int main()
    {
    
    
    
        if (SDL_Init(SDL_INIT_VIDEO) != 0) {
            printf("Unable to initialise SDL:  %s\n", SDL_GetError());
            return 1;
    
    }
    
    
    
    SDL_WM_SetCaption("Super Dungeon Lad", NULL);
    screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF);
    if (screen == NULL) {
        printf("Unsable to set video mode: %s\n", SDL_GetError());
        return 1;
    }
    
    
    printf("Success!\n");
    title();
    
    while (SDL_WaitEvent(&event) != 0) {
    switch (event.type) {
    case SDL_QUIT:
    exit(0);
    }
    }
    
    return 0;
    }
    
    title() {
        SDL_Surface *image;
        SDL_Rect src, dest;
        src.x = 0;
        src.y = 0;
        src.w = image->w;
        src.h = image->h;
        dest.h = 480;
        dest.w = 640;
        dest.x = 0;
        dest.y = 0;
        image = SDL_LoadBMP("gfx/titleScrn.bmp");
        SDL_BlitSurface(image, &src, screen, &dest);
        SDL_UpdateRect(screen, 0, 0, 0, 0);
        SDL_Flip(screen);
        SDL_FreeSurface(image);
      if (menuChoice == 1) {
          printf("New Game is selected\n");
          ttlMenu();
      }
      else if (menuChoice == 2) {
          printf("Continue is selected\n");
          ttlMenu();
      }
      else {
          ttlMenu();
      }
    }
    
    ttlMenu() {
        while (SDL_WaitEvent(&event) !=0) {
          switch (event.type) {
            case SDL_KEYDOWN:
              if (event.key.keysym.sym == SDLK_UP) {
                  menuChoice = 1;
                  title();
                  }
              else if (event.key.keysym.sym == SDLK_DOWN) {
                  menuChoice = 2;
                  title();
                  }
              else if (event.key.keysym.sym == SDLK_RETURN) {
                  printf("Enter key is pressed.\n");
                  if (menuChoice == 1) {
                      printf("New Game!\n");
                      newGame();
                      }
                  else if (menuChoice == 2) {
                      printf("Continue!\n");
                      loadGame();
                      }
                  else if (menuChoice == 0) {
                      printf("BZZT!  Nothing selected!\n");
                      ttlMenu();
                      }
            case SDL_QUIT:
            exit(0);
          }
    
    }}}
    
    newGame() {
        printf("This is where the game would start.\n");
    }
    
    loadGame() {
        printf("This is where the game would display available save data.\n");
    }
    I understand my code is probably not perfectly orderly, but I'm doing this for my own learning experience, anyway. I hope someone can be of some help. @_@;

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    I understand my code is probably not perfectly orderly
    Darn, I wanted to point that out .

    Code:
        SDL_Surface *image;
        SDL_Rect src, dest;
        src.x = 0;
        src.y = 0;
        src.w = image->w;
        src.h = image->h;
    You just happened to not get a segmentation violation while trying to access a surface that doesn't exist yet. So the w and h fields had whatever garbage image happened to be pointing at. My guess is that the next time you called title() image still pointed to the surface that you freed the previous time, which still had valid data in the w and h fields because FreeSurface is lazy like that.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    SOLVED: Freaky SDL conundrum

    Um... WOW. I'm not sure how I missed that... Thanks a ton! In all the craziness of editing my code, I guess I let my brain completely leak out of my ear. Now I can focus, again, on my goal! ^_^!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Securing .Net code (the 3rd party conundrum)
    By Mario F. in forum Tech Board
    Replies: 24
    Last Post: 03-07-2011, 01:55 PM
  2. freaky output!
    By eklavya8 in forum C Programming
    Replies: 4
    Last Post: 12-18-2008, 09:16 AM
  3. Some kind of freaky warning
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2004, 01:53 AM
  4. Freaky WIN2k network ops
    By jinx in forum Tech Board
    Replies: 5
    Last Post: 05-18-2004, 09:38 AM
  5. Dialog update conundrum
    By Keith Calder in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2002, 05:02 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21