Problem creating levels using inherited classes/polymorphism

Adam
  • Problem creating levels using inherited classes/polymorphism Adam

    I'm trying to write my level classes by having a base class that each level class inherits from...The base class uses pure virtual functions.

    My base class is only going to be used as a vector that'll have the inherited level classes pushed onto it...This is what my code looks like at the moment, I've tried various things and get the same result (segmentation fault).

    //level.h
    class Level
    {
      protected:
        Mix_Music *music;
        SDL_Surface *background;
        SDL_Surface *background2;
    
        vector<Enemy> enemy;
    
        bool loaded;
        int time;
      public:
        Level();
        virtual ~Level();
    
        int bgX, bgY;
        int bg2X, bg2Y;
        int width, height;
    
        virtual void load();
        virtual void unload();
    
        virtual void update();
        virtual void draw();
    };
    //level.cpp
    Level::Level()
    {
      bgX = 0;
      bgY = 0;
      bg2X = 0;
      bg2Y = 0;
      width = 2048;
      height = 480;
    
      loaded = false;
      time = 0; 
    }
    Level::~Level()
    {
    }
    //virtual functions are empty...
    

    I'm not sure exactly what I'm supposed to include in the inherited class structure, but this is what I have at the moment...

    //level1.h
    class Level1: public Level
    {
      public:
        Level1();
       ~Level1();
    
        void load();
        void unload();
    
        void update();
        void draw();
    };
    //level1.cpp
    Level1::Level1()
    {
    }
    Level1::~Level1()
    {
      enemy.clear();
    
      Mix_FreeMusic(music);
      SDL_FreeSurface(background);
      SDL_FreeSurface(background2);
    
      music = NULL;
      background = NULL;
      background2 = NULL;
    
      Mix_CloseAudio();
    }
    
    void Level1::load()
    { 
      music = Mix_LoadMUS("music/song1.xm");
      background = loadImage("image/background.png");
      background2 = loadImage("image/background2.png");
    
      Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 4096);
      Mix_PlayMusic(music, -1); 
    }
    void Level1::unload()
    {
    }
    //functions have level-specific code in them...
    

    Right now for testing purposes, I just have the main loop call Level1 level1; and use the functions, but when I run the game I get a segmentation fault. This is the first time I've tried writing inherited classes, so I know I'm doing something wrong, but I can't seem to figure out what exactly.

Tags
c++ levels objects polymorphism
Related questions and answers
  • = (WIDTH / 2) - 64; allegro_message("Initialzing ship class"); s->Init(x); int frame = 0; BITMAP* buffer = NULL; BITMAP* background = NULL; BITMAP* ship = NULL; SceCtrlData...(); } clear(buffer); draw_sprite(buffer, background, 0, 0); s->Draw(buffer, ship, frame); DrawBullets(buffer, m); masked_blit(buffer, screen, 0, 0, 0, 0, WIDTH, HEIGHT...("Couldn't load one or more sprites..."); return 0; } buffer = create_bitmap(WIDTH, HEIGHT); if (buffer == NULL) { allegro_message("Couldn't create buffer

  • in advance of first use. Here is a bit a sketch of the classes I am using: typedef unsigned int ResourceId; // Resource is an abstract data type. class Resource { Resource(); virtual...I've decided I want to write a central ResourceManager/ResourceCache class for my hobby game engine, but am having trouble designing a caching scheme. The idea is that the ResourceManager has... a level is running, // load/reload/unload might get called during level transitions. template <class T> ResourceGuard<T> &getResource(ResourceId resourceId

  • if the warning I am getting is the problem or if there is something else. Also, this is when the configuration being used is "Dedicated release", but I have also tried with "Dedicated Debug". It is my... the console, but cannot run the actual game. Here is what I did: Downloaded the source code from the game repo Downloaded and installed the DirectX SDK Purchased, downloaded and installed Doom 3 from... ------ Initializing File System ------ Loaded pk4 C:\Program Files (x86)\Steam\steamapps\common\doom 3\base\game00.pk4 with checksum 0x29cdb978 Loaded pk4 C:\Program Files (x86)\Steam\steamapps\common

  • the pipeline for rendering void init_pipeline() { // load the effect file D3DX10CreateEffectFromFile(L"effect.fx", 0, 0, "fx_4_0", 0, 0, device... iBuffer->Unmap(); } cube.h #ifndef CUBE_H #define CUBE_H #include "gameObject.h" class Cube : GameObject { private: //VERTEX* OurVertices; public: Cube(); ~Cube(); void Draw(); void Init...; scd.BufferDesc.Width = SCREEN_WIDTH; scd.BufferDesc.Height = SCREEN_HEIGHT; scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; scd.OutputWindow = hWnd; scd.SampleDesc.Count = 1; scd.SampleDesc.Quality = 0

  • (); glOrtho(0, Width, Height, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); Just in-case, here's my image loading code, I think that perhaps this may be were the problem lies somehow...I'm using SDL & openGL to render a tile-map. The issue is that the tile-map rendering is extremely messed up, and I'm just a bit unsure what I'm doing wrong exactly. It should just be the first tile being rendered, but I'm getting a blurred mess :S. My rendering code: glBindTexture(GL_TEXTURE_2D, texture); float texscale = 1.0f / (float)tileWidth; sourceX = sourceX / (float)tileSheetWidth

  • i think i just found the solution. 1) the problem is that backbuffer surface and source surface are of different formats - that is why exception was thrown. 2) the image path needed double slash "C\\......." ;) ================================= I have been googling for a while but still cant come up with a solution to the problem - D3DXGetImageInfoFromFile function just doesn't load any images..., Info.Format, D3DPOOL_SYSTEMMEM, &Surface, NULL); D3DXLoadSurfaceFromFile(Surface, NULL, NULL, wsPath.c_str(), NULL, D3DX_FILTER_NONE, 0, NULL); pd3dDevice -> GetBackBuffer(0, 0

  • onscreen, in the shape of the terrain rectangle, but there are no regular lines etc. Here's the code I use for rendering: void ShaderProgram::Draw() { using namespace AntiMatter; if( ! m... object from an array of CustomVertex objects. This array is said to be interleaved. It renders successfully with the following code: void VertexBufferObject::Draw() { if( ! m_bInitialized..._TRIANGLES, m_nNumIndices, GL_UNSIGNED_INT, ((char*)NULL + 0) ); glDisableClientState( GL_VERTEX_ARRAY ); glDisableClientState( GL_TEXTURE_COORD_ARRAY ); glDisableClientState( GL_NORMAL_ARRAY

  • I'm learning SDL. I'd like to create rectangular surface filled with a flat color (not an image). Below is my code -- it compiles fine, but doesn't work. I'm passing the function these parameters: SDL_Surface* m_screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); SDL_FillRect( m_screen, &m_screen->clip_rect, SDL_MapRGB(m_screen->format,0xFF, 0xFF, 0xFF) ); ... Button button(m_screen,0,0,50,50,255,0,0) ... ... Button::Button(SDL_Surface* screen,int x,int y,int w,int h,int R, int G, int B) { SDL_Rect box; SDL_Surface

  • #include <sstream> // gives access to rand function #include <cstdlib> //gives access to time functions #include <ctime> // mySTOPWATCH i think i'm gonna cry.... =') #include <... /////////////////////////////////////////////////////////////////////////////////////////////////////////////// class Sprite { private: string name; char symbol; float shield; int location[2]; bool alive; public: ///////////////////// Get and SET all the privates... destroy_shield() {shield--; if(shield<0) alive= false;} string get_name() {return name;}; string set_name(string aName) {name = aName;}; int* get_location(){return location;}; void

Data information