Jump to content
  • Advertisement
Sign in to follow this  
DasZaid

C++ -> Levels & Program Structures.

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm new to programming, and I've decided to make a game in C++ and SFML. Just a small one, but nonetheless, a game. It's a SHMUP using the same color pallet and resolution as the Original Game Boy, in the original game boy style. Here's a little concept screenie as to how I want it to look like (Although I'll probably scale it all to 2X so that it is easier to see and play):

concept.PNG

Now my two problems are designing an affective program structure (as well as how to draw one out), and loading/creating levels.

1) Affective Program Structure - I want to learn how to set one of these up to keep things nice and organized, and to avoid spaghetti code like in some of my older programs. If someone would be so kind to explain it out to me or link me to a tutorial on how to make one of these drawings on paper, and to design an effective one to use in my actual program. If I'm being too much of a noob, there's a video online here in which it shows how to design code and the code structure thing, but she never really explains how to draw one of these, and how to make your own to fit your programs needs.

2) Levels - Now, I want to have a system set up to where I create something like a level editor, I create a level in the level editor, and then it saves it to some binary file that then my program can load in, and delete on a whim. But I honestly have no idea where to start with this. I also want to create it all from scratch. If anyone could tell me how I can set one of these systems up, how it would work, ect ect... That would be lovely.

Thank you for taking the time to read this,
++Zaid

EDIT - Silly me, by looking further into the video I linked, I found the actual girls website, from there I looked in her tutorials section and found out she actually has a full written version of the tutorials, including something with levels. I'll have a look at that.

Share this post


Link to post
Share on other sites
Advertisement
I'm not going to go into #1 much learning how to design your program isn't always so easy as do "A, B, then C" For your program you're going to want to have your main loop, which goes through different parts of the program needed to run. For a Shumup, you'll basically do this:


while(bRunning)
{
ProcessLevel()
ProcessEnemies()
ProcessInput()
ProcessPlayer()
DrawVisible()
}

ProcessLevel()
{
// Check if we need to load a new level
// Check where we are in the level, and if we need to release a new background image, music, enemy, object, etc. into the proper lists
}

ProcessEnemies()
{
// Go through each enemy in EnemiesOnScreen list, and handle them (set their speeds, fire bullets, react to getting hit with bullet, etc.)
}

ProcessInput()
{
// Check if user input is available and process (move ship, fire bullet, etc.)
}

ProcessPlayer()
{
// Check if player has hit bullet, upgrade Object, update any special abilities it may have (speed timer for example)
}

DrawVisible()
{
// loop through all the visible object lists (backgrounds, Objects, Enemies, Bullet, and Player) and draw them on the screen
}



For the 2nd piece, I would think about using a configuration file (xml, for example, using tinyxml) which details all the levels, enemies, weapons, and upgrades. For the levels, have a Time parameter, which sets when in the level an entity is released on the screen (could be enemy, background, music, etc.), and the location it is released. For the enemies, have the images it will use, animation times , etc. Bullets give image, speed, strength, etc.

Anyway, that's how I did it, and it worked well.

Share this post


Link to post
Share on other sites

I'm not going to go into #1 much learning how to design your program isn't always so easy as do "A, B, then C" For your program you're going to want to have your main loop, which goes through different parts of the program needed to run. For a Shumup, you'll basically do this:


while(bRunning)
{
ProcessLevel()
ProcessEnemies()
ProcessInput()
ProcessPlayer()
DrawVisible()
}

ProcessLevel()
{
// Check if we need to load a new level
// Check where we are in the level, and if we need to release a new background image, music, enemy, object, etc. into the proper lists
}

ProcessEnemies()
{
// Go through each enemy in EnemiesOnScreen list, and handle them (set their speeds, fire bullets, react to getting hit with bullet, etc.)
}

ProcessInput()
{
// Check if user input is available and process (move ship, fire bullet, etc.)
}

ProcessPlayer()
{
// Check if player has hit bullet, upgrade Object, update any special abilities it may have (speed timer for example)
}

DrawVisible()
{
// loop through all the visible object lists (backgrounds, Objects, Enemies, Bullet, and Player) and draw them on the screen
}



For the 2nd piece, I would think about using a configuration file (xml, for example, using tinyxml) which details all the levels, enemies, weapons, and upgrades. For the levels, have a Time parameter, which sets when in the level an entity is released on the screen (could be enemy, background, music, etc.), and the location it is released. For the enemies, have the images it will use, animation times , etc. Bullets give image, speed, strength, etc.

Anyway, that's how I did it, and it worked well.


Nice, Im still learning game development on C++, I think Ill try this out and see for myself.

Share this post


Link to post
Share on other sites
Hey, concerning POINT 2 what I do is :

i have a class Named WorldMap. This class has all the important infos i need to know to draw the map , the npcs, the player etc.. it looks like this!


//

// WorldMap.h

// OpenGL

//

// Created by Markus Pfundstein on 9/10/11.

// Copyright 2011 Markus Pfundstein. All rights reserved.

//




#ifndef OpenGL_WorldMap_h

#define OpenGL_WorldMap_h




#include <OpenGLES/ES1/gl.h>

#include "Helpers.h"

#include "Constants.h"

#include "DisplayList.h"




class Texture2D;

class NPC;

class AnimatedChar;

class WorldMap

{

int nRows;

int nCols;



GLuint vbo;

GLuint texture;

GLuint texobjects;

GLuint *tbo;



DisplayList* displayList;

NPC *npcs;



TILE **tiles;



// for teleporting issues

Point2D newDestination;




char newWorld[30];

int8_t numSprites;

u_int8_t npcs_counter;




bool drawCollisionGrid;

bool isActive;



void drawCharacters(AnimatedChar &_char);

void parseLoadedFile(const char* filename);

void create_buffers();

void create_tbo(const GLfloat *texcoords, GLuint id);

public:




WorldMap();

~WorldMap();



bool initWorld(const char* filename);




void draw(const Rect2D &viewpoint, AnimatedChar &_char);



void addTileFlagAt(int i, int j, uint8_t flag, uint8_t flagprop1, uint8_t flagprop2, const char* optString);



void addNPCS(u_int8_t counter, NPC *newNPCs);



void createDisplayList(AnimatedChar &_char);



void suicide(Point2D newDest, const char* newMap);



bool getIsActive();



float get_world_width() const;

float get_world_height() const;



TILE& returnTileAt(int i, int j) const;

Point2D getPlayerEntryPoint() const;



char* getNewWorld();

Point2D getNewDestination();

};




#endif




Everytime i load a new Map , i call the initWorld(const char* filename) function.. this functions does everything for me. It allocates memory, i reads the world map file, it fills the structures, it places the npcs and events according to the script etc..

When the game gets the call to load a new map, i delete the instance of the world with the function suicide. this cleans all my memory , all pointers to zero etc, and than i start again from the initWorld function..

I started actually to write a small Editor first in which i can build my levels. After this was running well i wrote the engine itself for the game..


To Point 1:

the GameEngine runs also in a class, which gets initialized once when the game starts and kills when the game ends.. it has only a couple of functions which are called in a specific order..

the class looks like this:


//

// GameEngine.h

// OpenGL

//

// Created by Markus Pfundstein on 9/10/11.

// Copyright 2011 Markus Pfundstein. All rights reserved.

//




#ifndef OpenGL_GameEngine_h

#define OpenGL_GameEngine_h




#include <OpenGLES/ES1/gl.h>

#include "Helpers.h"

#include "Sprite2D.h"

#include "Texture2D.h"

#include "Player.h"

#include "WorldMap.h"

#include "Gui.h"

#include "Camera.h"

#include "Font.h"




#include <list>

using namespace std;




class GameEngine

{

private:

bool newMapLoaded;



u_int8_t currentGameState;



WorldMap *current_map;

Gui *gui;

Font *font_silon;

Player *player;

Camera *camera;




void adjust_scene();



void loadGUI();

bool loadNewMap(const char* filename);



public:

GameEngine();

~GameEngine();



bool initGameEngine();



void update_objects();

void draw_scene();

void handle_input(const Point2D& at_position);

void input_stop();

};




#endif






And the order of the function calls is like this




handle_input (runs on a different thread as it is an iOS application)

- consisting of handling input




update objects

- consisting of movement, animation etc

- collision detectiong




draw scene






Well I hope it helped a bit.




peace out







Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!