SDL classes error *Fixed*

Started by
2 comments, last by Lasko 18 years, 4 months ago
I am making classes to organize my program better, but now when i try to make the class for the playmat, its picture won't show up. DLCCGmain.cpp


#include "main.h"

#include "Playmat.h"

const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;

bool gamerunning = true;


SDL_Surface* screen  = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, 
       SDL_SWSURFACE | SDL_FULLSCREEN);
       
Playmat::Playmat()
{
    playmat_source.x = 0;
    playmat_source.y = 0;
    playmat_source.w = 804;
    playmat_source.h = 604;
    
    playmat_dest.x = 0;
    playmat_dest.y = 0;
    playmat_dest.w = 20;
    playmat_dest.h = 100;
}

Playmat::~Playmat()
{
}

void Playmat::Create()
{
     SDL_Surface* playmat = SDL_LoadBMP("data/playmat.bmp");
     
}

void Playmat::Destroy()
{
}

void Playmat::Draw()
{
     SDL_BlitSurface(playmat, &playmat_source, screen, &playmat_dest);
}

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    
    SDL_Surface* cardbg = SDL_LoadBMP("data/cardbg.bmp");
    SDL_Surface* aguhand = SDL_LoadBMP("data/agumon card(hand size).bmp");
    SDL_Surface* wargreyrow = SDL_LoadBMP("data/Wargreymon card (row size).bmp");
    SDL_Surface* weregarururow = SDL_LoadBMP("data/Weregarurumon card(row size).bmp");
    SDL_Surface* onlinepile = SDL_LoadBMP("data/online pile.bmp");
    
    SDL_Rect cardbg_source;
    cardbg_source.x = 0;
    cardbg_source.y = 0;
    cardbg_source.w = 248;
    cardbg_source.h = 329;
    
    SDL_Rect cardbg_dest;
    cardbg_dest.x = 450;
    cardbg_dest.y = 100;
    cardbg_dest.w = 20;
    cardbg_dest.h = 100;
    
    SDL_Rect aguhand_source;
    aguhand_source.x = 0;
    aguhand_source.y = 0;
    aguhand_source.w = 126;
    aguhand_source.h = 168;
    
    SDL_Rect aguhand_dest;
    aguhand_dest.x = 163;
    aguhand_dest.y = 420;
    aguhand_dest.w = 20;
    aguhand_dest.h = 100;
    
    SDL_Rect wargreyrow_source;
    wargreyrow_source.x = 0;
    wargreyrow_source.y = 0;
    wargreyrow_source.w = 117;
    wargreyrow_source.h = 156;
    
    SDL_Rect wargreyrow_dest;
    wargreyrow_dest.x = 170;
    wargreyrow_dest.y = 234;
    wargreyrow_dest.w = 20;
    wargreyrow_dest.h = 100;
    
    SDL_Rect weregarururow_source;
    weregarururow_source.x = 0;
    weregarururow_source.y = 0;
    weregarururow_source.w = 117;
    weregarururow_source.h = 156;
    
    SDL_Rect weregarururow_dest;
    weregarururow_dest.x = 296;
    weregarururow_dest.y = 234;
    weregarururow_dest.w = 20;
    weregarururow_dest.w = 100;
    
    SDL_Rect onlinepile_source;
    onlinepile_source.x = 0;
    onlinepile_source.y = 0;
    onlinepile_source.w = 150;
    onlinepile_source.h = 198;
    
    SDL_Rect onlinepile_dest;
    onlinepile_dest.x = 10;
    onlinepile_dest.y = 20;
    onlinepile_dest.w = 20;
    onlinepile_dest.h = 20;
    

    
    SDL_Event event;
    
    while(gamerunning)
    {
       if(SDL_PollEvent(&event))
       {
          if(event.type == SDL_QUIT)
          {
             gamerunning = false;
          }
       Uint8 *keysheld = SDL_GetKeyState(0);
       
             if( keysheld[SDLK_ESCAPE])
             {
             gamerunning=false;
             break;
             }
       }
       
       
       SDL_BlitSurface(onlinepile, &onlinepile_source, screen, &onlinepile_dest);
       SDL_BlitSurface(cardbg, &cardbg_source, NULL, &cardbg_dest);
       SDL_BlitSurface(aguhand, &aguhand_source, screen, &aguhand_dest);
       SDL_BlitSurface(wargreyrow, &wargreyrow_source, screen, &wargreyrow_dest);
       SDL_BlitSurface(weregarururow, &weregarururow_source, screen, &weregarururow_dest);
       SDL_Flip(screen);
    }
    
    SDL_Quit();
    
    return 0;
}


main.h

#ifndef _MAIN_H_
#define _MAIN_H_

#include <iostream>
#include <SDL/SDL.h>

#endif


Playmat.h

#ifndef PLAYMAT_H_
#define PLAYMAT_H_

class Playmat
{
      public:
             Playmat();
             ~Playmat();
             
             void Create();
             void Destroy();
             void Draw();
      private:
              SDL_Rect playmat_source;
              SDL_Rect playmat_dest;
              
              SDL_Surface* playmat;
              
};

#endif


really need help, thnx :D [Edited by - Lasko on December 3, 2005 1:34:26 PM]
Advertisement
When you are using SDL, you must initialize SDL first, create your display surface, load your surfaces, free your surfaces (not the main one though), and finally deiniitialize SDL. Right now, you have:
SDL_Surface* screen  = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,        SDL_SWSURFACE | SDL_FULLSCREEN);


In the global namespace, so SDL is not being initialized first, thus your program is not working correctly. You must first initialize SDL, then make a call, inside a function, to SDL_SetVideoMode, in your main() function.

It would also be a good idea to test your pointers after you call SDL_LoadBMP to ensure that those images were loaded. Note you can keep your screen variable global, just initialize it to 0, and not what you have it now.
I dont compeletly understand, i put the screen declaration after the SDL initiation, then how do i tell my class to get the screen from within the
main()?
Sry for the double post, but here is wat i thought i needed to do, but the picture is still not showing


#include "main.h"#include "Playmat.h"const int WINDOW_WIDTH = 800;const int WINDOW_HEIGHT = 600;bool gamerunning = true;           Playmat::Playmat(){    playmat_source.x = 0;    playmat_source.y = 0;    playmat_source.w = 804;    playmat_source.h = 604;        playmat_dest.x = 0;    playmat_dest.y = 0;    playmat_dest.w = 20;    playmat_dest.h = 100;}Playmat::~Playmat(){}void Playmat::Create(){     SDL_Surface* playmat = SDL_LoadBMP("data/playmat.bmp");     }void Playmat::Destroy(){}void Playmat::Draw(){     SDL_BlitSurface(playmat, &playmat_source, SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0,        SDL_SWSURFACE | SDL_FULLSCREEN), &playmat_dest);}int main(int argc, char **argv){    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);        SDL_Surface* screen  = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,        SDL_SWSURFACE | SDL_FULLSCREEN);        SDL_Surface* cardbg = SDL_LoadBMP("data/cardbg.bmp");    SDL_Surface* aguhand = SDL_LoadBMP("data/agumon card(hand size).bmp");    SDL_Surface* wargreyrow = SDL_LoadBMP("data/Wargreymon card (row size).bmp");    SDL_Surface* weregarururow = SDL_LoadBMP("data/Weregarurumon card(row size).bmp");    SDL_Surface* onlinepile = SDL_LoadBMP("data/online pile.bmp");        SDL_Rect cardbg_source;    cardbg_source.x = 0;    cardbg_source.y = 0;    cardbg_source.w = 248;    cardbg_source.h = 329;        SDL_Rect cardbg_dest;    cardbg_dest.x = 450;    cardbg_dest.y = 100;    cardbg_dest.w = 20;    cardbg_dest.h = 100;        SDL_Rect aguhand_source;    aguhand_source.x = 0;    aguhand_source.y = 0;    aguhand_source.w = 126;    aguhand_source.h = 168;        SDL_Rect aguhand_dest;    aguhand_dest.x = 163;    aguhand_dest.y = 420;    aguhand_dest.w = 20;    aguhand_dest.h = 100;        SDL_Rect wargreyrow_source;    wargreyrow_source.x = 0;    wargreyrow_source.y = 0;    wargreyrow_source.w = 117;    wargreyrow_source.h = 156;        SDL_Rect wargreyrow_dest;    wargreyrow_dest.x = 170;    wargreyrow_dest.y = 234;    wargreyrow_dest.w = 20;    wargreyrow_dest.h = 100;        SDL_Rect weregarururow_source;    weregarururow_source.x = 0;    weregarururow_source.y = 0;    weregarururow_source.w = 117;    weregarururow_source.h = 156;        SDL_Rect weregarururow_dest;    weregarururow_dest.x = 296;    weregarururow_dest.y = 234;    weregarururow_dest.w = 20;    weregarururow_dest.w = 100;        SDL_Rect onlinepile_source;    onlinepile_source.x = 0;    onlinepile_source.y = 0;    onlinepile_source.w = 150;    onlinepile_source.h = 198;        SDL_Rect onlinepile_dest;    onlinepile_dest.x = 10;    onlinepile_dest.y = 20;    onlinepile_dest.w = 20;    onlinepile_dest.h = 20;            SDL_Event event;        while(gamerunning)    {       if(SDL_PollEvent(&event))       {          if(event.type == SDL_QUIT)          {             gamerunning = false;          }       Uint8 *keysheld = SDL_GetKeyState(0);                    if( keysheld[SDLK_ESCAPE])             {             gamerunning=false;             break;             }       }                     SDL_BlitSurface(onlinepile, &onlinepile_source, screen, &onlinepile_dest);       SDL_BlitSurface(cardbg, &cardbg_source, NULL, &cardbg_dest);       SDL_BlitSurface(aguhand, &aguhand_source, screen, &aguhand_dest);       SDL_BlitSurface(wargreyrow, &wargreyrow_source, screen, &wargreyrow_dest);       SDL_BlitSurface(weregarururow, &weregarururow_source, screen, &weregarururow_dest);       SDL_Flip(screen);    }        SDL_Quit();        return 0;}




**********PROBLEM FIXED**********

[Edited by - Lasko on December 3, 2005 1:58:49 PM]

This topic is closed to new replies.

Advertisement