Files
DarkRP2D/src/DarkRP2D.cpp
2018-05-11 10:11:06 +02:00

142 lines
3.8 KiB
C++

#include "DarkRP2D.h"
#ifndef NDEBUG
#include "imgui.h"
#include "imgui_impl_sdl_gl3.h"
#endif
#include <GL/glew.h>
#include <iostream>
#include <entityx/entityx.h>
DarkRP2D::~DarkRP2D() {
std::cout << "Game got destroyed!" << std::endl;
#ifndef NDEBUG
ImGui_ImplSdlGL3_Shutdown();
ImGui::DestroyContext();
#endif
delete assets;
delete obj;
delete inputHandler;
delete camera;
delete renderer;
}
void DarkRP2D::create () {
std::cout << "Hello World!" << std::endl;
assets = new Assets();
assets->loadTexture("police_officer.png",true,"police_officer");
assets->loadShader("vert.shader","frag.shader",nullptr,"defaultShader");
assets->loadShader("bitmapvert.shader","bitmapfrag.shader",nullptr,"bitmapShader");
renderer = new Renderer();
std::vector<Vertex2D> vertices({
Vertex2D{glm::vec2(-0.5f,-0.5f), glm::vec2(0.0f,1.0f)},
Vertex2D{glm::vec2(0.5f,-0.5f), glm::vec2(1.0f,1.0f)},
Vertex2D{glm::vec2(0.5f,0.5f), glm::vec2(1.0f,0.0f)},
Vertex2D{glm::vec2(-0.5f,0.5f), glm::vec2(0.0f,0.0f)}
});
std::vector<unsigned int> indices({
0,1,3,
1,2,3
});
obj = new Mesh(vertices,indices,assets->getTexture("police_officer"));
inputHandler = new InputHandler();
font = new BitmapFont("arial.ttf");
camera = new OrtographicCamera();
entityx::EntityX ex;
entityx::Entity charakter = ex.entities.create();
charakter.assign<Position>();
#ifndef NDEBUG
//Setup IMGUI debugging
ImGui::CreateContext();
ImGui_ImplSdlGL3_Init(this->gWindow);
ImGui::StyleColorsDark();
#endif
}
void DarkRP2D::loop(unsigned int delta) {
static unsigned int accumulator = 0, ups = 0, fps = 0, acc = 0;
accumulator += delta;
acc += delta;
while(accumulator >= 17) {
accumulator -= 17;
update(delta);
ups++;
}
if(acc >= 1000) {
std::cout << "Updates per second: " << ups << " Frames per Second: " << fps << std::endl;
acc -= 1000;
ups = 0;
fps = 0;
}
render(delta);
fps++;
}
void DarkRP2D::update(unsigned int delta) {
SDL_Event event;
while(SDL_PollEvent(&event) != 0) {
Command* command = inputHandler->handleInput(&event);
if (command) {
command->execute();
}
}
//camera update
}
#ifndef NDEBUG
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
#endif
void DarkRP2D::render(unsigned int delta) {
renderer->clear();
#ifndef NDEBUG
ImGui_ImplSdlGL3_NewFrame(gWindow);
#endif
//Drawing
Shader defaultShader = assets->getShader("defaultShader");
defaultShader.SetMatrix4("MVP",camera->getCombinedMatrix(),true);
//Draw Mesh
renderer->draw(*obj,defaultShader);
//texttest
Shader bitmapShader = assets->getShader("bitmapShader");
font->drawText(bitmapShader, "Hallo du noob", 25.0f, 25.0f, 1.f, glm::vec3(0.5f,0.8f,0.2f));
#ifndef NDEBUG
// 1. Show a simple window.
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
{
static float f = 0.0f;
static int counter = 0;
ImGui::Text("-------------------OpenGL Info-------------------------");
ImGui::Text("GLEW Version: %s\n", glewGetString(GLEW_VERSION));
ImGui::Text(" Version: %s\n", glGetString(GL_VERSION));
ImGui::Text(" Vendor: %s\n", glGetString(GL_VENDOR));
ImGui::Text(" Renderer: %s\n", glGetString(GL_RENDERER));
ImGui::Text(" Shading: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
ImGui::Text("----------------------------------------------------------");
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
}
ImGui::Render();
ImGui_ImplSdlGL3_RenderDrawData(ImGui::GetDrawData());
#endif
SDL_GL_SwapWindow(gWindow);
}
void DarkRP2D::resize(int width, int height){
}