Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Level Planning

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Level Planning
abraker95
Member #11,800
March 2010

I'm making a game, and well... my code is messy I do not know how to plan out the level class. I've probably got A LOT of junk in the code that is not needed. The set level stuff is temporary until I'll make a level file where the level will be loaded from. Please help. :-[

class LEVEL
    {
     private:
        int num_of_tiles;
        int width, height;


     public:
        FRAME pic;              // nothing more than the coordinates, and a bitmap
        BITMAP *backround;
        TILE tiles[];          /* coordinates, number of tile(ex: 1-is that tile,
                                  2-is that), and a bitmap holding its picture

        void load_tileset(TILE tile)
        {
         if(tile.tile==1)
            tile.sprite=load_bitmap("sprites/tiles/cyber_v1.bmp" ,0);
        }

        void destroy_tileset(BITMAP *tile)
        {
         destroy_bitmap(tile);
        }

/**
        void load_level(LVLFILE level)
        {
         pic.window=create_bitmap(level.sizeX, level.sizeY);
         //backround=load_bitmap((string)level.backround_filename, 0);
        }
**/

        void draw_level(/**LVLFILE level**/)
        {
         pic.window= create_bitmap(width, height);
         pic.pos_x=0, pic.pos_y=0;

         load_tileset(tiles[0]); 
         for(int i=0; i<1; i++)
             draw_sprite(pic.window, /*tiles[i].sprite*/
                         load_bitmap("sprites/tiles/cyber_v1.bmp" ,0), 
                         tiles[i].pos_x, 
                         tile[i].pos_y);

         draw_sprite(buffer, pic.window, pic.pos_x, pic.pos_y);
         draw_sprite(screen, buffer, 0, 0);


         destroy_tileset(tiles[0].sprite); 
         destroy_bitmap(pic.window);
        }

        void set_tileset(TILE tile, int x, int y, int Tile)
        {
         tile.pos_x=x, tile.pos_y=y, tile.tile=Tile;
        }

        void set_level(int w, int h)  ///temporary
        {
         width=w, h=height;
        }
    };

jmasterx
Member #11,410
October 2009

The goal of object oriented programming is to see everything as objects and what each object should and should not do. It depends on the features of your game. It looks like your game is tile based, so then your level class can simply load and maintain your bitmaps, you could then expose some const iterators and these could be passed to your renderer. In my opinion, if you have public member variables for a level class, that sort of shouts bad design so look into that. Your level class probably shouldn't let anyone publicly load tiles, Probably the only public method should be load(const std::string filename);

If you want to get fancier with your resources, your load method could take a reference or pointer to a resource manager which you could then map names to ex:

resourceManager->getBitmapResource("Player"); which would return a NULL bitmap pointer if no player resource exists etc.

Just start thinking more object oriented and do not expose anything that is irrelevant to the client of the class. For example, I need a brain to talk to people but I don't expose it publicly, it's private!

Also, you really should not create an inline class or whatever you are doing there, this ain't Java ;)

//H

class Foo {
public:
void someMethod();
};


//CPP

void Foo::someMethod()
{
  //implement me
}

This will be important if you want to have cross dependencies some day, and it is proper practice.

abraker95
Member #11,800
March 2010

I'm always stuck on C, trying to goto Object-Oriented C++. Yes, I've read enough books as it is... Can you hint me at what is definitly needed or not needed, I'm having a feeling I'm putting to much junk into the class and it's disorganized.

jmasterx
Member #11,410
October 2009

Like I said, it depends on your design and needs, give me a bit more information on the goals of what the game can and cannot do, what a level in your game is (AI, scripting ? ) and I'll see what I can figure out from that.

Neil Roy
Member #2,229
April 2002
avatar

I still prefer C... but... when I design a new game, one of the first things I do is create a level editor. This helps me get my level design down pat so when I start on the actual game code, I have a level editor and level created for the game to load in. I know what data needs to be loaded etc.

Some things to consider for level data. The level graphics obviously, like tiles etc. Level dimensions, how large is it? Will it be a fixed size for all levels or do you want it to be variable in size? Where will the player start in your level? Will the start location be fixed, or different depending on the level? Where will enemies start on a level, same question, fixed, variable, random, will you need spawn points set etc. And extra things like objects that exist in the level that can be interacted with, picked up, put down etc. Once you know all this, I would work on a level editor. It will help you to see what you have missed as you develop the editor. terrain could be from 1 to 3 layers depending on the style of level, background layer that is always behind the players, middle layer that has impassible objects and a foreground layer that the player can walk behind, this layer could be removed when say a player enters a building so the roof would vanish but the other layers (floor, walls, objects) would still be visible.

Anyhow, plan this all out, create an editor... all this should be in a level class. Other considerations are things like how will a player complete the level? Will there be a tile they have to walk on to complete it or an object they need to interact with etc... in a level editor this would be easy to implement, a simple button you click on to set the tile etc... then click. In your class you would then have a variable that contains the tile location. Planning ahead really helps. Think about what you will need in your game, pull up notepad and start making notes, thinking about how you will store the data. With enough planning ahead, the programming will take care of itself... well, in C anyhow... C++... is another mess altogether. :P

Note: There's nothing wrong with "messy" code if you're the only one that will be viewing it. So long as you know what it does. I recommend using comments liberally in your code so you remember what it is doing (and so that if someone ever sees it, they will know). I have worked on code YEARS later where had I not commented it, I wouldn't have a clue what I was doing in it! heheh

---
“I love you too.” - last words of Wanda Roy

abraker95
Member #11,800
March 2010

The game itself is a fighter/platformer/adventure/shooter, something like the adventure mode of Smash Brawl.

The game should have
- AI
- Multiplayer
- Layers about 5 max (probably)
- The characters consist of "body parts" that are joined together (anyone see Counter
Strike?)
- Lighting effect
- I was thinking that each object should have 3 parts to them:
+ the sprite - what we see
+ the masking - for collision detection
+ the bump map - like 3d models have, but here it's on tile sets
and 2d characters, making them look 3d-ish via
the lighting effect.

The code I pasted above looks messy to me because I think the functions within the class are not smoothly working with each other. I also like optimizations, if the code can be simplified.

jmasterx
Member #11,410
October 2009

Looks like you plan to make a pretty sophisticated game so why not scrap the tile idea and just have bitmap objects and give them a center and just make your level in xml-like:

<Object>
<Layer> "2" </Layer>
<Name> "PokeBall" </Name>
<Bitmap> "Data/Graphics/Pokeball.png" </Bitmap>
<Location> "500,700" </Location>
</Object>

abraker95
Member #11,800
March 2010

XML ???
Can you please explain how to do so...

I updated the code... I value optimizations greatly, since when I ran the game, It ran somewhere ~5 fps

class LEVEL
    {
     private:
        int num_of_tiles;
        int width, height;
        int view_x, view_y;

     public:
        FRAME pic;            /* the code still can be cleaned this putting this
                                 and other things into the private.*/
        BITMAP *backround;
        TILE tiles[100000];   /*need help here, the # of tiles should equal to   
                                (width/44)*(height/44) where as 44 is the size of 
                                the tiles (may be changed)*/

        bool loaded;          // not sure if it will be needed


        BITMAP *load_tileset(TILE tile)
        {
         if(tile.tile==1)
            return load_bitmap("sprites/tiles/cyber_v1.bmp" ,0);
         if(tile.tile==2)
            return load_bitmap("sprites/tiles/cyber_v2.bmp" ,0);
        }

        void destroy_tileset(BITMAP *tile)
        {

         destroy_bitmap(tile);
        }
/**
        void load_level(LVLFILE level)
        {
         pic.window=create_bitmap(level.sizeX, level.sizeY);
         //backround=load_bitmap((string)level.backround_filename, 0); ///fix this
        }
**/
        void draw_level(/**LVLFILE level**/)
        {
         pic.window= create_bitmap(width, height);


         pic.pos_x=view_x, pic.pos_y=view_y;

         draw_sprite(pic.window, backround, 0, 0);


         for(int i=0; i<10; i++)
            {
             draw_sprite(pic.window, tiles[i].sprite, tiles[i].pos_x, 
                         tiles[i].pos_y);
            }

        void destroy_level()
        {
         for(int i=0; i<10; i++)
            {
             destroy_tileset(tiles[i].sprite);
            }


         destroy_bitmap(backround);
         destroy_bitmap(pic.window);
        }

        TILE set_tileset(int x, int y, int Tile)
        {
         TILE tile;
         tile.pos_x=x, tile.pos_y=y, tile.tile=Tile;
         tile.sprite=load_tileset(tile);
         return tile;
        }

        void set_level(int w, int h, int Vx, int Vy, BITMAP *Backround)  ///temporary
        {
         width=w, height=h;
         view_x=Vx, view_y=Vy;
         backround=Backround;
        }
    };

int x_view=0, y_view=0;
LEVEL demo;

    bool game()  //bool to tell if the game is running or not
    {
     //initialization

     int XPOS[10]={0,   44,  88,  132, 176, 220, 264, 308, 352, 1000};
     int YPOS[10]={556, 556, 556, 556, 556, 556, 556, 556, 556, 556};
     int TYPE[10]={1,   2,   1,   2,   1,   2,   1,   2,   1,   2, };

     for(int i=0; i<10; i++)
        {
         demo.tiles[i]=demo.set_tileset(XPOS[i], YPOS[i], TYPE[i]);
        }

     //set the size of the level, the position of the camera, and draw it
     demo.set_level(1200, 600, x_view, y_view, load_bitmap("sprites/backround.bmp"
                    ,0));


     //calculations and drawing
     if(key[KEY_LEFT])
        x_view-=10;
     if(key[KEY_RIGHT])
        x_view+=10;

     demo.draw_level();
     demo.destroy_level();

     //check if the person wants to quit
     if(key[KEY_ESC])
       {
        while(key[KEY_ESC]);
        return 0;
       }
     else
        return 1;
    }

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

abraker95 said:

        TILE tiles[100000];   /*need help here, the # of tiles should equal to   
                                (width/44)*(height/44) where as 44 is the size of 
                                the tiles (may be changed)*/

Since you can only declare an array using a compile time constant size, and you don't know the size at compile time, then you need to use new and delete to create a run time array.

#SelectExpand
1class Level { 2/// Same as before 3public : 4 Tile* tiles; 5 6 Level() : tiles(0) /*...*/ {} 7 8 ~Level() {Free();} 9 10 void Free() { 11 if (tiles) { 12 delete [] tiles; 13 tiles = 0; 14 } 15 } 16 17 void CreateTiles(int new_width , int new_height) { 18 Free(); 19 tiles = new Tile[new_width*new_height]; 20 size = new_width*new_height; 21 width = new_width; 22 height = new_height; 23 InitTiles(); 24 } 25 26 void InitTiles() { 27 for (int y = 0 ; y < height ; ++y) { 28 for (int x = 0 ; x < width ; ++x) { 29 tiles[y*width + x] = /*...*/ 30 } 31 } 32 } 33 34};

Also, I would suggest only loading each bitmap once, and then only let your Tile class have a reference to a BITMAP*, but not be responsible for freeing it.

jmasterx
Member #11,410
October 2009

Allocating and deallocating your own array on the heap is fine, but I personally prefer a std::vector. You can then reserve for the size of your map, and then fill it up.

std::vector<Tile> tiles;
tiles.reserve(NUMBER_OF_UNIQUE_TILES);

I would then have

std::vector<Tile*> map;
map.reserve(width * height);

and then you place your tiles accordingly,
Maybe by reading the level file,
If you need layers just use nested std::vectors.

For your sprites (player, enemy, etc) you could store those tiles into a <std::string, Tile*> std::map which could be convenient for networking multiplayer or something.

abraker95
Member #11,800
March 2010

Ok, but the code is still slow. How do you make it faster?

Tobias Dammers
Member #2,604
August 2002
avatar

Run it through a profiler. That's a tool designed to measure the amount of time spent in each function, and the number of times it gets called. A profiler will give you detailed information about where the performance bottlenecks are.
With these results, you can typically tell quite easily what the problem is; usually, you can reduce the number of times a certain function gets called.

Without any more information, here's a few common rookie mistakes that can lead to severe slowdown:

  • loading bitmaps inside the main loop (loading files is slow, you want to load them all before the main loop starts, and only once)

  • drawing the entire tilemap instead of just the visible part

  • doing inefficient collision checks (e.g. checking your character against every individual tile in the level)

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

abraker95
Member #11,800
March 2010

Where can I get this Profiler? ???

jmasterx
Member #11,410
October 2009

Try Very Sleepy, http://www.codersnotes.com/sleepy it is the one I use.

abraker95
Member #11,800
March 2010

Ok, but exactly how do you use?

Neil Roy
Member #2,229
April 2002
avatar

Which compiler do you use? CodeBlocks comes with the option to install a profiling plugin that works fairly well. You just set up to compile your project (Project->Build Options->Compiler Flags) with the "Profile code when executed" checked. Then when you run it at least once you get a file named "gmon.out". You run a plugin named "Code Profiler" and select that file and you'll get profiling information.

---
“I love you too.” - last words of Wanda Roy

abraker95
Member #11,800
March 2010

I tried implementing the new type into the code, and it gave me errors such as:

'new' cannot appear in a constant-expression
ISO C++ forbids declaration of 'tiles' with no type
forbids initialization of member 'tiles'
making 'tiles' static
ISO C++ forbids in-class initialization of non-const static member 'tiles'
declaration of 'int LEVEL::tiles'
conflicts with previous declaration 'TILE* LEVEL::tiles' :-/

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

abraker95
Member #11,800
March 2010

You have to excuse me, I don't quit know how to use code formatting correctly. :(

Here you go:

#include <allegro.h>
//#include <stdlib.h> //for random
//#include <time.h>  //to seed random
#include <iostream>
using namespace std;

//{
    int Active_menu=0;

    BITMAP *buffer;

    struct FRAME
    {
     int pos_x, pos_y;
     BITMAP *window;
    };

    struct TILE
    {
     int pos_x, pos_y;
     int tile;
     BITMAP *sprite;
    };
/*
    struct LVLFILE
    {
     const int sizeX, sizeY;
     char backround_filename[];
     int map[];
    };
*/

    class LEVEL
    {
     private:
        int num_of_tiles;
        int width, height;
        BITMAP *backround;
        FRAME pic;

     public:
        int view_x, view_y;
        TILE tiles[100000];

        BITMAP *load_tileset(TILE tile)
        {
         if(tile.tile==1)
            return load_bitmap("sprites/tiles/cyber_v1.bmp" ,0);
         if(tile.tile==2)
            return load_bitmap("sprites/tiles/cyber_v2.bmp" ,0);
        }

        void destroy_tileset(BITMAP *tile)
        {
         destroy_bitmap(tile);
        }
/**

        void load_level(LVLFILE level)
        {
         pic.window=create_bitmap(level.sizeX, level.sizeY);
         //backround=load_bitmap((string)level.backround_filename, 0); ///fix this
        }

**/
        void draw_level()
        {
         pic.pos_x=view_x, pic.pos_y=view_y;

         draw_sprite(pic.window, backround, 0, 0);

         for(int i=0; i<10; i++)
            {
             draw_sprite(pic.window, tiles[i].sprite, tiles[i].pos_x, tiles[i].pos_y);
            }

         draw_sprite(buffer, pic.window, pic.pos_x, pic.pos_y);
         draw_sprite(screen, buffer, 0, 0);
        }

        void destroy_level()
        {
         for(int i=0; i<10; i++)
            {
             destroy_tileset(tiles[i].sprite);
            }
         destroy_bitmap(backround);
         destroy_bitmap(pic.window);
        }

        TILE set_tileset(int x, int y, int Tile)
        {
         TILE tile;
         tile.pos_x=x, tile.pos_y=y, tile.tile=Tile;
         tile.sprite=load_tileset(tile);
         return tile;
        }

        void set_level(int w, int h, int Vx, int Vy, BITMAP *Backround)  ///temporary
        {
         width=w, height=h;
         pic.window= create_bitmap(width, height);
         view_x=Vx, view_y=Vy;
         backround=Backround;
        }
    };

    void set_game(LEVEL *level) // need pointer to pass it to the actual level variable as a refrence, otherwise it won't work
    {
     //initialization

     int XPOS[20]={0,   44,  88,  132, 176, 220, 264, 308, 352, 1000, 0,   44,  88,  132, 176, 220, 264, 308, 352, 1000};
     int YPOS[20]={556, 556, 556, 556, 556, 556, 556, 556, 556, 556,  512, 512, 512, 512, 512, 512, 512, 512, 512, 512};
     int TYPE[20]={1,   2,   1,   2,   1,   2,   1,   2,   1,   2,    1,   2,   1,   2,   1,   2,   1,   2,   1,   2,};

     for(int i=0; i<10; i++)
        {
         level->tiles[i]=level->set_tileset(XPOS[i], YPOS[i], TYPE[i]);
        }

     //set the size of the level, the position of the camera, and draw it
     level->set_level(1200, 600, 0, 0, load_bitmap("sprites/backround.bmp" ,0));
    }

    bool update_game(LEVEL *level)
    {
     //calculations and drawing
     if(key[KEY_LEFT])
        level->view_x+=1;
     if(key[KEY_RIGHT])
        level->view_x-=1;

     level->draw_level();

     //check if the person wants to quit
     if(key[KEY_ESC])
       {
        while(key[KEY_ESC]);
        return 0;
       }
     else
        return 1;
    }

    struct BUTTON
    {
     public:
     int x_cor, y_cor;
     BITMAP *icon;  /// icon_pressed?

     bool button_pressed()
     {
      bool pressed=0;
      if((mouse_b & 1) && (mouse_x>=x_cor && mouse_x<=icon->w+x_cor) && (mouse_y>=y_cor && mouse_y<=icon->h+y_cor))
         {
          while((mouse_b & 1) && (mouse_x>=x_cor && mouse_x<=icon->w+x_cor) && (mouse_y>=y_cor && mouse_y<=icon->h+y_cor));
          return 1;
         }
      else
         return 0;
     }   /// fix button draging
    };

    /**  All The menu buttons **/
    //{
        // Main Menu buttons
        BUTTON start, controls, options, about, Exit,

        // Option Menu buttons
        audio, graphics, OMback,  /// profile?

        // Control Menu buttons
        CMback,    /// player control input?

        // About Menu buttons
        AMback,

        // Start Menu buttons
        New, load, SMback;
    //}

    /** All the frames/levels **/
    //{
        FRAME main_menu, start_menu, controls_menu, options_menu, about_menu;
        LEVEL demo;
    //}

//}
/**                                     END OF DECLARATIONS                                 **/

//{
    void init_buttons()
    {
     int coor=0;
     start.x_cor=0,              start.y_cor=coor,           start.icon=load_bitmap("sprites/buttons/start.bmp", 0);
     controls.x_cor=0,           controls.y_cor=coor+50,     controls.icon=load_bitmap("sprites/buttons/controls.bmp",0);
     options.x_cor=0,            options.y_cor=coor+100,     options.icon=load_bitmap("sprites/buttons/options.bmp",0);
     about.x_cor=0,              about.y_cor=coor+150,       about.icon=load_bitmap("sprites/buttons/about.bmp",0);
     Exit.x_cor=0,               Exit.y_cor=coor+200,        Exit.icon=load_bitmap("sprites/buttons/exit.bmp",0);

     // Option Menu buttons
     audio.x_cor=0,              audio.y_cor=coor,              audio.icon=load_bitmap("sprites/buttons/audio.bmp", 0);
     graphics.x_cor=0,           graphics.y_cor=coor+50,        graphics.icon=load_bitmap("sprites/buttons/graphics.bmp", 0);
     OMback.x_cor=0,             OMback.y_cor=coor+100,         OMback.icon=load_bitmap("sprites/buttons/back.bmp", 0);

     // Controls Menu buttons
     CMback.x_cor=0,             CMback.y_cor=coor,             CMback.icon=load_bitmap("sprites/buttons/back.bmp",0);

     // About Menu buttons
     AMback.x_cor=0;             AMback.y_cor=coor;             AMback.icon=load_bitmap("sprites/buttons/back.bmp",0);

     // Start Menu buttons
     New.x_cor=0,                New.y_cor=coor,              New.icon=load_bitmap("sprites/buttons/new.bmp",0);
     load.x_cor=0,               load.y_cor=coor+50,          load.icon=load_bitmap("sprites/buttons/load.bmp",0);
     SMback.x_cor=0,             SMback.y_cor=coor+100,       SMback.icon=load_bitmap("sprites/buttons/back.bmp",0);
    }

    void destroy_buttons()  // need it to delete the bitmap or it will take up all the memory by the use of the while loop(declaring it all the time without delting it)
    {
     destroy_bitmap(start.icon);
     destroy_bitmap(controls.icon);
     destroy_bitmap(options.icon);
     destroy_bitmap(about.icon);
     destroy_bitmap(Exit.icon);

     // Option Menu buttons
     destroy_bitmap(audio.icon);
     destroy_bitmap(graphics.icon);
     destroy_bitmap(OMback.icon);

     // Controls Menu buttons
     destroy_bitmap(CMback.icon);

    // About Menu buttons
     destroy_bitmap(AMback.icon);

    //Start Menu buttons
     destroy_bitmap(New.icon);
     destroy_bitmap(load.icon);
     destroy_bitmap(SMback.icon);
    }

    /// The menus
    //{
       int draw_start_menu()
       {
        init_buttons();

        start_menu.pos_x=0, start_menu.pos_y=0;
        start_menu.window=create_bitmap(SCREEN_W,SCREEN_H);

        draw_sprite(start_menu.window, New.icon, New.x_cor, New.y_cor);
        draw_sprite(start_menu.window, load.icon, load.x_cor, load.y_cor);
        draw_sprite(start_menu.window, SMback.icon, SMback.x_cor, SMback.y_cor);

        if(New.button_pressed())
            return 1;
        else if(load.button_pressed())
            cout<<"load"<<endl;
        else if(SMback.button_pressed())
            return 3;
        else;

        draw_sprite(buffer, start_menu.window, start_menu.pos_x, start_menu.pos_y);
        destroy_bitmap(start_menu.window);

        destroy_buttons();
        return 0;
       }

       int draw_options_menu()
       {
        init_buttons();

        options_menu.pos_x=0, options_menu.pos_y=0;
        options_menu.window=create_bitmap(SCREEN_W,SCREEN_H);

        draw_sprite(options_menu.window, audio.icon, audio.x_cor, audio.y_cor);
        draw_sprite(options_menu.window, graphics.icon, graphics.x_cor, graphics.y_cor);
        draw_sprite(options_menu.window, OMback.icon, OMback.x_cor, OMback.y_cor);

        if(audio.button_pressed())
           cout<<"audio"<<endl;
        else if(graphics.button_pressed())
           cout<<"graphics"<<endl;
        else if(OMback.button_pressed())
           return 3;
        else;

        draw_sprite(buffer, options_menu.window, options_menu.pos_x, options_menu.pos_y);
        destroy_bitmap(options_menu.window);

        destroy_buttons();
        return 0;
       }

       int draw_controls_menu()
       {
        init_buttons();

        controls_menu.pos_x=0, controls_menu.pos_y=0;
        controls_menu.window=create_bitmap(SCREEN_W,SCREEN_H);

        draw_sprite(controls_menu.window, CMback.icon, CMback.x_cor, CMback.y_cor);

        if(CMback.button_pressed())
           return 1;

        draw_sprite(buffer, controls_menu.window, controls_menu.pos_x, controls_menu.pos_y);
        destroy_bitmap(controls_menu.window);

        destroy_buttons();
        return 0;
       }

       int draw_about_menu()
       {
        init_buttons();

        about_menu.pos_x=0, about_menu.pos_y=0;
        about_menu.window=create_bitmap(SCREEN_W,SCREEN_H);

        draw_sprite(about_menu.window, AMback.icon, AMback.x_cor, AMback.y_cor);

        if(AMback.button_pressed())
            return 1;
        else;

        draw_sprite(buffer, about_menu.window, about_menu.pos_x, about_menu.pos_y);
        destroy_bitmap(about_menu.window);
        return 0;

       }

       int draw_main_menu()
       {
        init_buttons();

        main_menu.pos_x=0, main_menu.pos_y=0;
        main_menu.window=create_bitmap(SCREEN_W, SCREEN_H);

        draw_sprite(main_menu.window, start.icon, start.x_cor, start.y_cor);
        draw_sprite(main_menu.window, controls.icon, controls.x_cor, controls.y_cor);
        draw_sprite(main_menu.window, options.icon, options.x_cor, options.y_cor);
        draw_sprite(main_menu.window, about.icon, about.x_cor, about.y_cor);
        draw_sprite(main_menu.window, Exit.icon, Exit.x_cor, Exit.y_cor);

        if(start.button_pressed())
           return 1;
        else if(controls.button_pressed())
           return 2;
        else if(options.button_pressed())
           return 3;
        else if(about.button_pressed())
           return 4;
        else if(Exit.button_pressed())
           return 5;
        else;

        draw_sprite(buffer, main_menu.window, main_menu.pos_x, main_menu.pos_y);
        destroy_bitmap(main_menu.window);

        destroy_buttons();
        return 0;
       }
    //}

    bool update_menu()
    {
     int menu_picked;
     if(Active_menu==0)
       {
        menu_picked=draw_main_menu();

        if(menu_picked==1)
           Active_menu=1;
        else if(menu_picked==2)
           Active_menu=2;
        else if(menu_picked==3)
           Active_menu=3;
        else if(menu_picked==4)
           Active_menu=4;
        else if(menu_picked==5)
           return 0;
        else;

      }
     else if(Active_menu==1)
       {
        menu_picked=draw_start_menu();
        if(menu_picked==1)
          {
           set_game(&demo);
           while(update_game(&demo));
           demo.destroy_level();
          }

        if(menu_picked==3)
            Active_menu=0;
       }
     else if(Active_menu==2)
       {
        menu_picked=draw_controls_menu();

        if(menu_picked==1)
           Active_menu=0;
       }

     else if(Active_menu==3)
       {
        menu_picked=draw_options_menu();

        if(menu_picked==3)
           Active_menu=0;
       }
     else if(Active_menu==4)
       {
        menu_picked=draw_about_menu();

        if(menu_picked==1)

          Active_menu=0;
       }

     else;
     return 1;
    }

//}
/**                                 END OF GLOBAL INITIALIZATION                                 **/

int main()
{
    allegro_init();
    install_keyboard();
    install_mouse();
    set_color_depth(24);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
    show_mouse(screen);
    buffer=create_bitmap(SCREEN_W, SCREEN_H);

    while(!key[KEY_ESC])
         {
          rectfill(buffer, 0, 0, SCREEN_W, SCREEN_H, makecol(0, 0, 0));

          //do everything
          if(!update_menu())
             break;

          draw_sprite(screen, buffer, 0, 0);
         }

    return 0;
}
END_OF_MAIN();

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Post the full code you are using, and the full errors that you get(including line numbers and source files).

Did you use a single <code></code> tag for all of that code? If you did, I think Matthew's formatting is broken somehow.

To show line numbers and the file name, use code tags like this :
<code start="1" name="Main.cpp"></code>

abraker95
Member #11,800
March 2010

Hmmm... I just copied the tags from the formatting help button. it said
code start="1">//code </code but it showed an empty box, so I changed it to
code start="0">//code </code, and allowed some code to show but I had repeatedly start and end it. But thanks I tried it your way and it worked. * >:( stupid help >:(*

So what's with the code?

EDIT: ???????? Why can't I post new threads (start a new thread?)
EDIT 1: Nevermind about the array, I got it. I've got a new question: how to init a list to that array
ex: int array[num]={0,1,2,3,4,5,6,7,8,9};

LennyLen
Member #5,313
December 2004
avatar

abraker95 said:

I've got a new question: how to init a list to that array

Like this: int array[]={0,1,2,3,4,5,6,7,8,9};

abraker95
Member #11,800
March 2010

In function 'void set_game(LEVEL*)'
error: conflicting declaration 'int TYPE []'
error: 'TYPE' has a previous declaration as 'int* TYPE'

How to do it after doing:

int *array= new int[num_elem];

LennyLen
Member #5,313
December 2004
avatar

abraker95 said:

How to do it after doing:
int *array= new int[num_elem];

You can't. But if you want to assign a specific number of items like that, then there's no need to use new like that.

abraker95
Member #11,800
March 2010

But there should be a way init a dynamic array with a list. :o

EDIT: Nevermind I found the solution myself while playing around with type casting:
int *XPOS; XPOS= new int [level->total_tiles];
XPOS=(int []){0, 44, 88, 132, 176, 220, 264, 308, 352, 1000, 0, 44, 88, 132, 176, 220, 264, 308, 352, 1000};

I have a new problem now; drawing the backround takes ALOT of processing time since the for loops take time to go through the drawing. Is there a way to optimize this?

for(int y=0; y<pic.window->h; y+=backround->h)
         {
          for(int x=0; x<pic.window->w; x+=backround->w)
             {
              draw_sprite(pic.window, backround, x, y);
             }
         }

 1   2 


Go to: