void Engine::initSystems() { cout << "Initializing Systems" << endl; shared_ptr<BaseSystem> input(new InputSystem()); systemList["input"] = input; // BaseSystem *player_input = new PlayerInputSystem(); // player_input->init(this); // systemList["player_input"] = player_input; //Here we add the different input types; // InputSystem* myinput = (InputSystem*)systemList["input"]; // input->inputList.push_back(input); // BaseSystem *animation = new AnimationSystem(12); // animation->init(this); // systemList["animation"] = animation; shared_ptr<BaseSystem> Terrain(new TerrainSystem(screenWidth, screenHeight)); systemList["terrain"] = Terrain; shared_ptr<BaseSystem> Camera(new CameraSystem(screenWidth, screenHeight)); systemList["camera"] = Camera; shared_ptr<BaseSystem> render(new RenderSystem(screenWidth, screenHeight, "Dynasty Warriors"));//"media/aztlan_bg_2x.png"); systemList["render"] = render; for(auto it = systemList.begin(); it != systemList.end(); ++it) { it->second->init(this); } }
Terrain loadMap(std::string p_fichier) { std::ifstream fichier(p_fichier.c_str(), std::ios::in ); // on ouvre en lecture std::vector<std::pair<int,int>> temp; if(fichier) // si l'ouverture a fonctionné { std::string line; // déclaration d'une chaîne qui contiendra la ligne lue while(getline(fichier, line)) // tant que l'on peut mettre la ligne dans "contenu" { std::istringstream iss; iss.str(line); std::string i, j; iss >> i >> j ; temp.push_back(std::pair<int,int>(std::atoi(i.c_str()), std::atoi(j.c_str()))); } fichier.close(); } std::cout << temp.size() <<"_"<< std::endl; return Terrain(temp); }
sint32 World::GetMaxShieldsFromTerrain() { static sint32 max_prod = 0; if (max_prod != 0) return max_prod; for (sint32 i = 0; i < g_theTerrainDB->NumRecords(); i++) { max_prod = std::max(max_prod, Terrain(g_theTerrainDB->Get(i)).GetShield()); } return max_prod; }
sint32 World::GetAvgGoldFromTerrain() { static sint32 avg_gold = 0; if (avg_gold != 0) return avg_gold; for (sint32 i = 0; i < g_theTerrainDB->NumRecords(); i++) { avg_gold += Terrain(g_theTerrainDB->Get(i)).GetGold(); } avg_gold /= g_theTerrainDB->NumRecords(); return avg_gold; }
sint32 World::GetAvgShieldsFromTerrain() { static sint32 avg_prod = 0; if (avg_prod != 0) return avg_prod; for (sint32 i = 0; i < g_theTerrainDB->NumRecords(); i++) { avg_prod += Terrain(g_theTerrainDB->Get(i)).GetShield(); } avg_prod /= g_theTerrainDB->NumRecords(); return avg_prod; }
void TerrainTests::testCreate() { Terrain* t = OGRE_NEW Terrain(mSceneMgr); Image img; img.load("terrain.png", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Terrain::ImportData imp; imp.inputImage = &img; imp.terrainSize = 513; imp.worldSize = 1000; imp.minBatchSize = 33; imp.maxBatchSize = 65; t->prepare(imp); // don't load, this requires GPU access //t->load(); OGRE_DELETE t; }
World::World(double _width, double _height) : BoundedObject(Pd(), Qd(), BoundingBox(Pd(-_width/2, -_height/2, 0), Pd(_width,0,0), Pd(0,_height,0), Pd(_width,_height,0), Pd(), Pd(), Pd(), Pd(_width/2, _height/2, HIGH)), Assets::WorldMaterial) { ObjectHandle tHandle; tHandle = Terrain(_width, _height); terrain = TO(Terrain, tHandle); children.insert(tHandle); ObjectHandle hudHandle; hudHandle = HUD(640, 480); hud = TO(HUD, hudHandle); children.insert(hudHandle); children.insert(Sky((int) _width, (int) _height)); width = _width; height = _height; }
int main(void) { //Initialize allegro-------------------------------- MainUtility.InitAllegro(); if (MainUtility.Get_GameOver()) { return -1; } srand(time(NULL)); //variables----------------------------------------- const int FPS = 60; //Allegro variables--------------------------------- ALLEGRO_EVENT_QUEUE *Event_Queue = NULL; ALLEGRO_TIMER *Timer = NULL; Event_Queue = al_create_event_queue(); Timer = al_create_timer(1.0 / FPS); Display MainDisplay(Event_Queue); if (!MainDisplay.TestDisplay()) { return -1; } ALLEGRO_BITMAP *DungeonTiles = al_load_bitmap("Test.png"); ALLEGRO_BITMAP *AIImage = al_load_bitmap("AI_Sprite.png"); ALLEGRO_BITMAP *PlayerImage = al_load_bitmap("Player_Sprite.png"); ALLEGRO_BITMAP *BowImage = al_load_bitmap("Bow_Sprite.png"); ALLEGRO_BITMAP *SwordImage = al_load_bitmap("Sword_Sprite.png"); Player MainPlayer(PlayerImage, SwordImage, BowImage, MainDisplay.Get_ScreenWidth(), MainDisplay.Get_ScreenHeight(), Event_Queue); Camera MainCamera(Event_Queue); AI_Group TestAIGroup; // Instance of an AI_Group TerrainGenerator Terrain(1); Terrain.generateTerrain(); DungeonGenerator Dungeon(Event_Queue, &MainPlayer); Dungeon.GenerateDungeon(MainDisplay); MainPlayer.SetXPosition(Dungeon.GetStartPosition().x()); MainPlayer.SetYPosition(Dungeon.GetStartPosition().y()); TestAIGroup.RandomSetup(4, Dungeon, AIImage); // Generates 4 AI with random attributes in the group. Their spawn points will also be set randomly. TestAIGroup.GetPathToPlayer(MainPlayer); // For testing, generate a path to the player's start position from each AI ALLEGRO_EVENT_SOURCE ProjectileEvent; al_init_user_event_source(&ProjectileEvent); al_register_event_source(Event_Queue, al_get_timer_event_source(Timer)); al_register_event_source(Event_Queue, al_get_keyboard_event_source()); al_register_event_source(Event_Queue, al_get_mouse_event_source()); al_start_timer(Timer); //Main game loop------------------------------------ while (!MainUtility.Get_GameOver()) { ALLEGRO_EVENT ev; al_wait_for_event(Event_Queue, &ev); MainPlayer.EventHandler(ev, MainCamera.GetMouseXWorldCoordinate(), MainCamera.GetMouseYWorldCoordinate()); TestAIGroup.ProcessAll(ev, MainPlayer); // Process each AI in the group MainCamera.EventHandler(ev, MainPlayer.GetXPosition(), MainPlayer.GetYPosition()); MainDisplay.Event_Handler(ev); Dungeon.Event_Handler(ev); // Collide with AI if (TestAIGroup.CollideWithAI(MainPlayer.GetCollisionXBoundOne(), MainPlayer.GetCollisionYBoundOne())) MainPlayer.MovementCollidingBoundOne(); if (TestAIGroup.CollideWithAI(MainPlayer.GetCollisionXBoundTwo(), MainPlayer.GetCollisionYBoundTwo())) MainPlayer.MovementCollidingBoundTwo(); // Hit the AI TestAIGroup.HitAI(MainPlayer.GetWeaponHitBoxXBoundOne(), MainPlayer.GetWeaponHitBoxYBoundOne(), MainPlayer.GetWeaponDamage()); TestAIGroup.HitAI(MainPlayer.GetWeaponHitBoxXBoundTwo(), MainPlayer.GetWeaponHitBoxYBoundTwo(), MainPlayer.GetWeaponDamage()); if (Dungeon.Get_Map()->CheckMapCollision(Vec2f(MainPlayer.GetCollisionXBoundOne(), MainPlayer.GetCollisionYBoundOne()))) MainPlayer.MovementCollidingBoundOne(); if (Dungeon.Get_Map()->CheckMapCollision(Vec2f(MainPlayer.GetCollisionXBoundTwo(), MainPlayer.GetCollisionYBoundTwo()))) MainPlayer.MovementCollidingBoundTwo(); //Code Dealing with drawing to the screen goes within this if statement if (al_is_event_queue_empty(Event_Queue)) { Dungeon.Draw(); MainPlayer.DrawPlayer(); //Terrain.draw(); TestAIGroup.DrawAll(); // Draw all AI (magenta squares). Their generated paths will also be drawn (small magenta circles). //Draw display last MainDisplay.Draw(); } } //Game Ending-------------------------------------- return 0; }
// Keyboard function void keyboard(unsigned char key, int x, int y) { switch (key) { case 'q': // Exit window case 'Q': exit(0); break; case'[': camPos[1] += 1; // Go upwards in scene break; case ']': camPos[1] -= 1; // Go downwards in scene break; case 'r': case 'R': // Randomize terrain t = Terrain(tSize,80); t.setWireMode(wMode); t.setColMode(colMode); t.generate(); t.vertexNormal(); refresh = true; break; case 'w': case 'W': // Turn on/off Wiremode if (wMode == 1){ wMode++; } else if (wMode == 2){ wMode++; }else if (wMode == 3){ wMode = 1; } t.setWireMode(wMode); break; case 'a': case 'A': // Increase size and randomize terrain if (tSize < 300){ tSize+=50; t = Terrain(tSize,80); t.generate(); t.vertexNormal(); t.setWireMode(wMode); } break; case 's': case 'S': // Decrease and randomize terrain if (tSize > 50){ tSize-=50; t = Terrain(tSize,80); t.generate(); t.vertexNormal(); t.setWireMode(wMode); } break; case 'l': case 'L': // Turn on lighting if (lightOn == false){ lightOn = true; glEnable(GL_LIGHTING); } else if (lightOn == true){ lightOn = false; glDisable(GL_LIGHTING); } t.setLight(lightOn); break; case 'd': case 'D': // Switch between each type of shading (Flat / Gourad) // If light mode is requested, turn the light on if (lightOn == true) { glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); // Dictates what type of shading to use if (shadeMode == 0){ shadeMode = 1; glShadeModel(GL_SMOOTH); } else if (shadeMode == 1){ shadeMode = 0; glShadeModel(GL_FLAT); } } break; case 'c': // Enables/Disables colour mode case 'C': if (colMode == true){ colMode = false; } else if (colMode == false){ colMode = true; } t.setColMode(colMode); break; } glutPostRedisplay(); }
/* main function - program entry point */ int main(int argc, char** argv) { cout << "--LEGEND-- \n"; cout << "Key 'q': Exits program \n"; cout << "Key 'r': Random terrain generation \n"; cout << "Key 'w': Enables/Disables full polygon/wireframe/full poly-wireframe mode \n"; cout << "Key 'a': Generate new enlarged terrain (up to 300x300) \n"; cout << "Key 's': Generate new smaller terrain (down to 50x50) \n"; cout << "Key 'l': Enables/Disables lighting \n"; cout << "Key 'd': Switches between flat/gourad shading \n"; cout << "Key 'c': Switches on colour mode \n"; cout << "\n"; cout << "Please input the terrain size and number of iterations \n"; cout << "Size: \n"; int size; cin >> size; cout << "Iterations: \n"; int cuts; cin >> cuts; // Generates a terrain on program startup t = Terrain(size,cuts); t.generate(); t.vertexNormal(); glutInit(&argc, argv); //starts up GLUT //** 2-D OVERVIEW WINDOW **/ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(300, 300); glutInitWindowPosition(910, 100); glutCreateWindow("2-D Overview"); //creates the window glutDisplayFunc(overviewDisplay); //registers "overviewDisplay" as the display callback function //** MAIN TERRAIN WINDOW ** glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800, 800); glutInitWindowPosition(100, 100); glutCreateWindow("Terrain Generator"); //creates the window glutDisplayFunc(display); //registers "display" as the display callback function glutKeyboardFunc(keyboard); // registers "keyboard" callback glutSpecialFunc(special); // registers "special key" callback glutReshapeFunc(reshape); // registers "reshape" callback init(); glutMainLoop(); //starts the event loop return (0); //return may not be necessary on all compilers }
# include <GL/gl.h> # include <GL/glu.h> # include <GL/freeglut.h> #endif #endif #include "Terrain.h" #include <stdlib.h> #include <iostream> // Defines all of our variables float camPos[] = { 50, 60, 50 }; Terrain t = Terrain(300,300); // Create new instance of Terrain class float L1Pos[4] = { 10, 50, 10, 0 }; //store position of light int wMode = 1; int tSize = 50; bool lightOn = false; int shadeMode = 0; int height, width; bool colMode = false; bool refresh = false; // Keyboard function void keyboard(unsigned char key, int x, int y) {
//---------------------------------------------------------------------------- void Terrains::CreateScene () { // Create the root of the scene. mScene = new0 Node(); // Load and initialize the sky dome. It follows the camera. std::string skyMeshName = Environment::GetPathR("SkyDomePNT2.wmvf"); Visual::PrimitiveType type; VertexFormat* vformat; VertexBuffer* vbuffer; IndexBuffer* ibuffer; Visual::LoadWMVF(skyMeshName, type, vformat, vbuffer, ibuffer); mSkyDome = new0 TriMesh(vformat, vbuffer, ibuffer); mScene->AttachChild(mSkyDome); APoint skyPosition = mCamera->GetPosition(); skyPosition[2] = 0.0f; mSkyDome->LocalTransform.SetTranslate(skyPosition); mSkyDome->LocalTransform.SetUniformScale(mCamera->GetDMax()); Texture2DEffect* skyEffect = new0 Texture2DEffect( Shader::SF_LINEAR_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT); std::string skyTextureName = Environment::GetPathR("SkyDome.wmtf"); Texture2D* skyTexture = Texture2D::LoadWMTF(skyTextureName); skyTexture->GenerateMipmaps(); mSkyDome->SetEffectInstance(skyEffect->CreateInstance(skyTexture)); // Load the height field and create the terrain. vformat = VertexFormat::Create(3, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 1); // For lower-resolution terrain, change the paths to Height64/Color64 or // Height32/Color32. std::string heightName = ThePath + "Data/Height128/height"; std::string colorName = ThePath + "Data/Color128/color"; mTerrain = new0 Terrain(heightName, vformat, mCamera); mScene->AttachChild(mTerrain); // The effect that is shared across all pages. std::string effectFile = Environment::GetPathR("BaseMulDetailFogExpSqr.wmfx"); TerrainEffect* terrainEffect = new0 TerrainEffect(effectFile); std::string detailName = Environment::GetPathR("Detail.wmtf"); Texture2D* detailTexture = Texture2D::LoadWMTF(detailName); detailTexture->GenerateMipmaps(); ShaderFloat* fogColorDensity = new0 ShaderFloat(1); (*fogColorDensity)[0] = 0.5686f; (*fogColorDensity)[1] = 0.7255f; (*fogColorDensity)[2] = 0.8353f; (*fogColorDensity)[3] = 0.0015f; // Attach an effect to each page. Preload all resources to video memory. // This will avoid frame rate stalls when new terrain pages are // encountered as the camera moves. const int numRows = mTerrain->GetRowQuantity(); const int numCols = mTerrain->GetColQuantity(); for (int r = 0; r < numRows; ++r) { for (int c = 0; c < numCols; ++c) { TerrainPage* page = mTerrain->GetPage(r, c); char suffix[32]; sprintf(suffix, ".%d.%d.wmtf", r, c); std::string colorTextureName = colorName + std::string(suffix); Texture2D* colorTexture = Texture2D::LoadWMTF(colorTextureName); colorTexture->GenerateMipmaps(); VisualEffectInstance* instance = terrainEffect->CreateInstance( colorTexture, detailTexture, fogColorDensity); page->SetEffectInstance(instance); mRenderer->Bind(page->GetVertexBuffer()); mRenderer->Bind(page->GetVertexFormat()); mRenderer->Bind(page->GetIndexBuffer()); mRenderer->Bind(colorTexture); } } }
void Run() { SetKey(GLFW_KEY_ESCAPE, std::bind(&Terminate)); deltaTime = 1.0 / 60.0; InitializeWindow(); //Init values and objects // Build the broadphase btBroadphaseInterface* broadphase = new btDbvtBroadphase(); // Set up the collision configuration and dispatcher btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); // The actual physics solver btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; // The world. btDiscreteDynamicsWorld* world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); world->setGravity(btVector3(0, -9.82f*METER, 0)); Terrain testObj = Terrain(world); Object* terrain = &testObj; objects.push_back(terrain); Anatomy testOrg = Anatomy(world); Object* testOrgP = &testOrg; objects.push_back(testOrgP); //timer info for loop double t = 0.0f; double currentTime = glfwGetTime(); double accumulator = 0.0f; glfwPollEvents(); //stop loop when glfw exit is called glfwSetCursorPos(mainThread, SCREEN_SIZE.x / 2.0f, SCREEN_SIZE.y / 2.0f); while (!glfwWindowShouldClose(mainThread)) { double newTime = glfwGetTime(); double frameTime = newTime - currentTime; //std::cout << "FPS:: " <<1.0f / frameTime << std::endl; //setting up timers if (frameTime > 0.25) { frameTime = 0.25; } currentTime = newTime; accumulator += frameTime; //# of updates based on accumulated time while (accumulator >= deltaTime) { MouseInput();//update mouse change glfwPollEvents(); //executes all set input callbacks CameraInput(); //bypasses input system for direct camera manipulation Update(deltaTime); //updates all objects based on the constant deltaTime. world->stepSimulation(deltaTime, 10); t += deltaTime; accumulator -= deltaTime; } //draw glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw(); glfwSwapBuffers(mainThread); } //cleanup delete world; delete solver; delete dispatcher; delete collisionConfiguration; delete broadphase; }
Terrain MapHandler::getNextTerrain() { //find the appropriate tile graphic and store it Terrain temp = Terrain(); Frame frame; switch(map[xTer][yTer]) { //trees (picks from a selection of trees) case 'T': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 32, 64, 0, 0, 32, 64, SHEET_TERRAIN); frame.addStandBox(5, 40, 22, 20); temp.addFrame(frame); break; case '0': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 128, 0, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '1': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 144, 0, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '2': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 160, 0, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '3': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 176, 0, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '4': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 128, 16, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '5': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 144, 16, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '6': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 160, 16, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '7': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 176, 16, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '8': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 128, 32, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case '9': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 144, 32, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case 'A': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 160, 32, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case 'B': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 176, 32, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case 'C': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 128, 48, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case 'D': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 144, 48, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case 'E': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 160, 48, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; case 'F': frame = Frame(0, xTer*TILES_X, yTer*TILES_Y+UI_HEIGHT, 16, 16, 176, 48, 16, 16, SHEET_TERRAIN); frame.addStandBox(0, 0, 16, 16); temp.addFrame(frame); break; } //increment x/y counters xTer++; if (xTer >= 30) { xTer = 0; yTer++; } return temp; }