Bought AppGameKit from steam today and are just familiarizing myself with it. I've made a biomes routine to generate terrain for a level and are testing it, but when looping through the terrain to print the generated content it freezes the application
I am using MS Visual C++ 10, here is the code:
biomes.h
#pragma once
#include <vector>
#include <time.h>
#include "biomestypes.h"
class BioMes
{
public:
BioMes(void);
~BioMes(void);
std::vector<std::vector<terrainType>> getBioMes();
private:
float calculateVolume();
std::vector<terrainInfo> makeBiomeInfo();
std::vector<std::vector<terrainType>> terrain;
std::vector<std::vector<vegitation>> veg;
environmentType environment;
composition comp;
float volume, mass, gravity;
};
biomes.cpp
#include "BioMes.h"
BioMes::BioMes(void)
{
volume = calculateVolume();
float percentageFromVolume = volume * 20 / 100;
int twentyPercent = (int)volume - (int)percentageFromVolume;
std::vector<terrainInfo> biomeInfo = makeBiomeInfo();
std::vector<terrainType> tmpVector;
for (int i = 0; i < volume; i++)
{
tmpVector.clear();
for (int x = 0; x < twentyPercent; x++)
{
terrainType nearest = terrainType::FLAT;
int dist = INT_MAX;
for (int z = 0; z < biomeInfo.size(); z++)
{
int xdiff = biomeInfo[z].x - i;
int ydiff = biomeInfo[z].y - x;
int cdist = xdiff*xdiff + ydiff*ydiff;
if (cdist < dist)
{
nearest = biomeInfo[z].type;
dist = cdist;
}
}
tmpVector.push_back(nearest);
}
terrain.push_back(tmpVector);
}
}
BioMes::~BioMes(void)
{
terrain.clear();
}
float BioMes::calculateVolume()
{
srand (time(NULL));
float value = rand() % 1000 + 100;
return value;
}
std::vector<terrainInfo> BioMes::makeBiomeInfo()
{
std::vector<terrainInfo> biomeInfo;
terrainInfo info;
info.type = terrainType::HILLS;
info.x = 26;
info.y = 26;
biomeInfo.push_back(info);
info.type = terrainType::MOUNTAINS;
info.x = 32;
info.y = 25;
biomeInfo.push_back(info);
info.type = terrainType::FLAT;
info.x = 10;
info.y = 5;
biomeInfo.push_back(info);
info.type = terrainType::CANYON;
info.x = 13;
info.y = 18;
biomeInfo.push_back(info);
return biomeInfo;
}
std::vector<std::vector<terrainType>> BioMes::getBioMes()
{
return terrain;
}
template.h
#ifndef _H_SMACKIT_
#define _H_SMACKIT_
// Link to GDK libraries
#include "AGK.h"
#include "Character.h"
#include "BioMes.h"
#define DEVICE_WIDTH 1024
#define DEVICE_HEIGHT 768
#define DEVICE_POS_X 32
#define DEVICE_POS_Y 32
#define FULLSCREEN false
// Global values for the app
class app
{
public:
// constructor
app() { memset ( this, 0, sizeof(app)); }
// main app functions - mike to experiment with a derived class for this..
void Begin( void );
void Loop( void );
void End( void );
private:
Character *character;
std::vector<std::vector<terrainType>> terrain;
};
extern app App;
#endif
// Allow us to use the LoadImage function name
#ifdef LoadImage
#undef LoadImage
#endif
template.cpp
// Includes
#include "template.h"
// Namespace
using namespace AGK;
app App;
void app::Begin(void)
{
agk::SetVirtualResolution (1024, 768);
agk::SetClearColor( 151,170,204 ); // light blue
agk::SetSyncRate(60,0);
agk::SetScissor(0,0,0,0);
agk::SetWindowTitle("Hello World");
character = new Character;
BioMes biomes;
terrain = biomes.getBioMes();
agk::CreateSprite(1, character->getImage());
agk::SetSpriteSize(1, character->getW(), character->getH());
for (int i = 0; i < terrain.size(); i++)
{
for (int x = 0; x < terrain[i].size(); x++)
{
switch(terrain[i][x]) {
case terrainType::FLAT:
agk::Print ( 'O' );
break;
case terrainType::HILLS:
agk::Print ( '#' );
break;
case terrainType::CANYON:
agk::Print ( 'Z' );
break;
case terrainType::MOUNTAINS:
agk::Print ( '^' );
break;
default:
agk::Print ( '?' );
break;
}
}
}
}
void app::Loop (void)
{
agk::Print( agk::ScreenFPS() );
agk::Sync();
}
void app::End (void)
{
delete character;
}
Basically the loop in app::Begin is what causes the freeze, but it is out of the application loop so the function should run only once rather than every frame?