Advertisement

problems with graphics engine

Started by March 04, 2002 09:55 PM
4 comments, last by evilclown 22 years, 9 months ago
My graphics engine works if i comment out one line. If I uncomment it, it appears to work but it doesn''t. The window closes after the x is pressed, but it''s still in the ctrl-alt-del menu. I have to end the task before I can link my program. Do you see anything wrong in my code?

//graphics.h

#include <vector>
#include <string>

class graphics {
  public:
    graphics();
    ~graphics();

    bool init(int, int, int);
    int LoadSurface(std::string);
    void FreeSurfaces();
    int get_width();
    int get_height();
    int get_bpp();
    void blit_tile(int, int, int, int, int, int, int);
    void render_begin();
    void render_end();

  private:
    vector surfaces;
    SDL_Surface* screen;

    int screen_width;
    int screen_height;
    int screen_bpp;
};

graphics::graphics() {
  //initialize class

  screen_width = 0;
  screen_height = 0;
  screen_bpp = 0;
}

graphics::~graphics() {
  //free graphics
  FreeSurfaces();
}

void graphics::FreeSurfaces() {
  while(surfaces.size() > 0) {
    SDL_FreeSurface(surfaces[surfaces.size() - 1]);
  }
}

bool graphics::init(int width, int height, int bpp) {
  if(SDL_Init(SDL_INIT_VIDEO) < 0) {
    return false;
  }
  screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);
  if(screen == NULL) {
    return false;
  }
  screen_width = width;
  screen_height = height;
  screen_bpp = bpp;
  return true;
}

int graphics::LoadSurface(std::string file) {
  SDL_Surface* surface;
  int index;

  index = surfaces.size();
  surface = SDL_LoadBMP(file.data());
  if(surface == NULL) {
    cerr << "could not load: " << file << endl << SDL_GetError() << endl;
    return -1;
  }
  surfaces.push_back(surface);
  SDL_FreeSurface(surface);
  return index;
}

int graphics::get_width() {
  return screen_width;
}

int graphics::get_height() {
  return screen_height;
}

int graphics::get_bpp() {
  return screen_bpp;
}

void graphics::blit_tile(int index, int tilex, int tiley, int srcx, int srcy, int width, int height) {
  SDL_Rect dest;
  SDL_Rect src;

  dest.x = tilex * width;
  dest.y = tiley * height;
  dest.w = width;
  dest.h = height;

  src.x = srcx * width;
  src.y = srcy * height;
  src.w = width;
  src.h = height;

  SDL_BlitSurface(surfaces[index], &src, screen, &dest);

}

void graphics::render_begin() {
  //fill a blank screen
  SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 125, 0, 0));
}

void graphics::render_end() {
  //update screen
  SDL_UpdateRect(screen, 0, 0, 0, 0);
}
 
..

//main.cpp

#include "SDL.h"
#include "graphics.h"

graphics game_graphics;
bool bdone = false;

int main(int argc, char *argv[]) {

  SDL_Event event;
  int stileset;

  //init
  if(game_graphics.init(640, 480, 16)==false) {
    cerr << "Error" << endl << SDL_GetError() << endl;
    return -1;
  }

  //stileset = game_graphics.LoadSurface("tileset.bmp");
  //this is the line that is a problem

  do {
    //events
    while(SDL_PollEvent(&event)) {
      switch(event.type) {
        case SDL_QUIT:
          bdone = true;
          break;
      }
    }

    //input
    //math
    //render
    game_graphics.render_begin();
      //game_graphics.blit_tile(stileset, 0, 0, 0, 0, 32, 32);
      //going to blit, but can''t until surface problem is fixed
    game_graphics.render_end();

  }while(!bdone);

  //done
  SDL_Quit();
  cout << "done." << endl;
  return 0;
}
 
I know the one line in the class declaration is supposed to be vector surfaces;

Am I not using vectors properly? If I comment out this line
surfaces.push_back(surface);  

It doesn't stay in the ctrl-alt-del menu.

edit: the board thinks I'm using html. It should say vector<SDL_Surface*> surface; where it says vector surfaces;

Edited by - evilclown on March 6, 2002 11:54:22 AM
Advertisement
I''m not too hot on vectors but if the vector is just a group of pointers to surfaces, using
SDL_FreeSurface(surface); 
is getting rid of the surface but leaving the pointer in vector. Should''nt you get rid of the surfaces only when the program ends.

,Jay
Commenting out SDL_FreeSurface(surface); doesn''t fix it. The only thing that does fix it is commenting out surfaces.push_back(surface);. I can use surfaces, but I can''t get them working in a vector. Any other ideas?
I don't like the look of your graphics::FreeSurfaces() function. I would've written it like this:

void graphics::FreeSurfaces() {

int NumSurfaces;

NumSurfaces = surfaces.size();

for (int i=0; i LESS THAN NumSurfaces; i++)
SDL_FreeSurface( surfaces.at(i) );

}

It thinks I'm trying to use html, so I'll write some of it out.

Edited by - DaWanderer on March 7, 2002 11:22:59 PM
I still can't see the reason for it. Unless you want a vector of actual surfaces, in which case you're copying the SDL_Surface class when you pushback and freeing up the original, bit
point(er)less really.

Have you tested the surface before you pushback ?

Also as your getting the index before you pushback, the index is going to represent the tile one before the current. That makes the first pushback Index=-1 (or null) when vector=empty.

I hope I'm making cents.

,Jay

Edited by - Jason Zelos on March 8, 2002 3:10:21 PM

This topic is closed to new replies.

Advertisement