diff --git a/.vscode/launch.json b/.vscode/launch.json index 1b6bf0a..50cbc4a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,7 +7,7 @@ "program": "${workspaceFolder}/build/src/hello.elf", "args": [], "stopAtEntry": false, - "cwd": "${fileDirname}", + "cwd": "${workspaceFolder}/romdisk", "environment": [], "externalConsole": false, "MIMode": "gdb", @@ -31,7 +31,7 @@ "program": "${workspaceFolder}/build/src/gl.elf", "args": [], "stopAtEntry": false, - "cwd": "${fileDirname}", + "cwd": "${workspaceFolder}/romdisk", "environment": [], "externalConsole": false, "MIMode": "gdb", diff --git a/src/cube.cc b/src/cube.cc index 961fcef..6a823e8 100644 --- a/src/cube.cc +++ b/src/cube.cc @@ -4,24 +4,31 @@ #include #include #include +#define SANIC_LOCATION "/rd/sanic.png" +#define CUBE_LOCATION "/rd/cube.obj" #else #define STB_IMAGE_IMPLEMENTATION -#include #include +#include #include +#define SANIC_LOCATION "./sanic.png" +#define CUBE_LOCATION "./cube.obj" #endif #include #include +#include #include + #include "engine/engine.hh" class Cube : public Engine { unsigned int texture; tinyobj::ObjReader reader; - bool parseObj(std::string objFile, tinyobj::ObjReader& reader); + bool parseObj(std::string objFile, tinyobj::ObjReader& reader, + char* message); void cube(); void displayStuff(); void gameLoop() override { displayStuff(); } @@ -40,10 +47,10 @@ void Cube::model() { for (size_t s = 0; s < shapes.size(); s++) { // Loop over faces(polygon) size_t index_offset = 0; - std::vector vertexes {}; - std::vector normals {}; - std::vector textureCoords {}; - + std::vector vertexes{}; + std::vector normals{}; + std::vector textureCoords{}; + for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]); @@ -62,7 +69,6 @@ void Cube::model() { vertexes.push_back(vertex); // glVertex3f(vx, vy, vz); // drawCalls += 1; - // Check if `normal_index` is zero or positive. negative = no // normal data @@ -107,21 +113,23 @@ void Cube::model() { glNormalPointer(GL_FLOAT, 0, &normals[0]); glTexCoordPointer(2, GL_FLOAT, 0, &textureCoords[0]); glDrawArrays(GL_TRIANGLES, 0, vertexes.size()); - // glDrawElements(GL_TRIANGLES, shapes[s].mesh.indices.size(), GL_UNSIGNED_INT, &shapes[s].mesh.indices[0]); + // glDrawElements(GL_TRIANGLES, shapes[s].mesh.indices.size(), + // GL_UNSIGNED_INT, &shapes[s].mesh.indices[0]); drawCalls += 1; } - printf("Draw Calls: %i\n", drawCalls); + // printf("Draw Calls: %i\n", drawCalls); drawCalls = 0; // glDrawArrays(GL_QUADS, 0, susSize); } -bool Cube::parseObj(std::string objFile, tinyobj::ObjReader& reader) { +bool Cube::parseObj(std::string objFile, tinyobj::ObjReader& reader, + char* message) { tinyobj::ObjReaderConfig readerConfig; readerConfig.mtl_search_path = "./"; if (!reader.ParseFromFile(objFile, readerConfig)) { if (!reader.Error().empty()) { - printf("TinyOBJReader: %s\n", reader.Error().c_str()); + sprintf(message, "TinyOBJReader: %s", reader.Error().c_str()); return false; } } @@ -138,7 +146,6 @@ void Cube::displayStuff() { } void Cube::initScreen() { - glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, 640.0f / 480.0f, 0.1f, 100.0f); @@ -154,21 +161,23 @@ void Cube::initScreen() { int width, height, nr_channels; unsigned char* data = - stbi_load("/rd/sanic.png", &width, &height, &nr_channels, 0); + stbi_load(SANIC_LOCATION, &width, &height, &nr_channels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - #ifdef _arch_dreamcast +#ifdef _arch_dreamcast glGenerateMipmap(GL_TEXTURE_2D); - #endif +#endif } else { printf("Texture failed to load.\n"); } stbi_image_free(data); - if (!parseObj("/rd/cube.obj", reader)) return; + char* message = new char; + if (!parseObj(CUBE_LOCATION, reader, message)) + throw std::runtime_error(message); } int main() { diff --git a/src/engine/controller/dreamcastController.cc b/src/engine/controller/dreamcastController.cc index c8393b1..a710200 100644 --- a/src/engine/controller/dreamcastController.cc +++ b/src/engine/controller/dreamcastController.cc @@ -5,9 +5,11 @@ DreamcastController::DreamcastController() { } bool DreamcastController::InitializeController() { - controller = maple_enum_type(0, MAPLE_FUNC_CONTROLLER); - if (this->controller) { - controller_state = (cont_state_t*)maple_dev_status(controller); + maple_device_t* cont = nullptr; + cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER); + if (cont->valid) { + this->controller = cont; + this->controller_state = (cont_state_t*)maple_dev_status(controller); return true; } else { controller = nullptr; diff --git a/src/engine/controller/linuxController.cc b/src/engine/controller/linuxController.cc index bffd309..5ca8b70 100644 --- a/src/engine/controller/linuxController.cc +++ b/src/engine/controller/linuxController.cc @@ -1,8 +1,18 @@ #include "linuxController.hh" #include +#include +#include bool LinuxController::InitializeController() { + std::ifstream gameControllerDB; + gameControllerDB.open("./gamecontrollerdb.txt"); + if (gameControllerDB.is_open()) { + printf("Opened gamecontrollerdb.txt\n"); + std::stringstream mappings; + mappings << gameControllerDB.rdbuf(); + glfwUpdateGamepadMappings(mappings.str().c_str()); + } if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) { glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState); return true; diff --git a/src/engine/dreamcastEngine.cc b/src/engine/dreamcastEngine.cc index 194e18a..ca0907f 100644 --- a/src/engine/dreamcastEngine.cc +++ b/src/engine/dreamcastEngine.cc @@ -21,4 +21,8 @@ void DreamcastEngine::initializeController() { void DreamcastEngine::SwapBuffers() { glKosSwapBuffers(); } + +bool DreamcastEngine::ShouldWindowClose() { + return false; +} #endif diff --git a/src/engine/dreamcastEngine.hh b/src/engine/dreamcastEngine.hh index 9a1830e..cec8151 100644 --- a/src/engine/dreamcastEngine.hh +++ b/src/engine/dreamcastEngine.hh @@ -6,5 +6,6 @@ class DreamcastEngine : public NativeEngine { void initializeController() override; void initializeEngine() override; void SwapBuffers() override; + bool ShouldWindowClose() override; }; #endif diff --git a/src/engine/engine.cc b/src/engine/engine.cc index 46379ff..d50161e 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -20,16 +20,20 @@ void Engine::initializeEngine() { #endif engine->initializeEngine(); initScreen(); - controller = engine->controller; } void Engine::initializeController() { engine->initializeController(); + controller = engine->controller; } void Engine::SwapBuffers() { engine->SwapBuffers(); } +bool Engine::ShouldWindowClose() { + return engine->ShouldWindowClose(); +} + void Engine::initScreen() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glViewport(0, 0, 640, 480); @@ -63,7 +67,7 @@ void Engine::deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame, void Engine::initializeGameLoop() { struct timeval beginningOfFrame, endOfFrame; - while (1) { + while (!ShouldWindowClose()) { deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame); } } diff --git a/src/engine/engine.hh b/src/engine/engine.hh index 55dc8b8..bad26ac 100644 --- a/src/engine/engine.hh +++ b/src/engine/engine.hh @@ -19,13 +19,15 @@ class Engine { bool thirtyfps; unsigned int buttonsPressed; + Controller* controller = nullptr; + virtual void initScreen(); - void printSystemInformation(); virtual void gameLoop(){}; + void printSystemInformation(); void pressButton(Controller::Button button, std::function callback); void glVertex3fNormalized(float x, float y, float z); void SwapBuffers(); - Controller* controller = nullptr; + bool ShouldWindowClose(); private: template diff --git a/src/engine/linuxEngine.cc b/src/engine/linuxEngine.cc index 0c7d231..280f07f 100644 --- a/src/engine/linuxEngine.cc +++ b/src/engine/linuxEngine.cc @@ -41,4 +41,8 @@ void LinuxEngine::SwapBuffers() { glfwSwapBuffers(window); glfwPollEvents(); } + +bool LinuxEngine::ShouldWindowClose() { + return glfwWindowShouldClose(this->window); +} #endif diff --git a/src/engine/linuxEngine.hh b/src/engine/linuxEngine.hh index ab1e734..80c6334 100644 --- a/src/engine/linuxEngine.hh +++ b/src/engine/linuxEngine.hh @@ -8,6 +8,7 @@ class LinuxEngine : public NativeEngine { void initializeController() override; void initializeEngine() override; void SwapBuffers() override; + bool ShouldWindowClose() override; GLFWwindow* window = nullptr; }; #endif diff --git a/src/engine/nativeEngine.cc b/src/engine/nativeEngine.cc deleted file mode 100644 index e69de29..0000000 diff --git a/src/engine/nativeEngine.hh b/src/engine/nativeEngine.hh index e252618..8c114d3 100644 --- a/src/engine/nativeEngine.hh +++ b/src/engine/nativeEngine.hh @@ -6,6 +6,7 @@ class NativeEngine { virtual void initializeController(){}; virtual void initializeEngine(){}; virtual void SwapBuffers(){}; + virtual bool ShouldWindowClose(){return false;}; Controller* controller = nullptr; }; #endif diff --git a/src/gl.cc b/src/gl.cc index a640c4f..2e53b22 100644 --- a/src/gl.cc +++ b/src/gl.cc @@ -55,6 +55,7 @@ void Glcc::displayStuff() { } void Glcc::process_input() { + controller->PollController(); pressButton(Controller::Button::DPAD_LEFT, [&]() { speed -= 1.0f; }); pressButton(Controller::Button::DPAD_RIGHT, [&]() { speed += 1.0f; }); pressButton(Controller::Button::A, [&]() { thirtyfps = !thirtyfps; }); @@ -63,6 +64,7 @@ void Glcc::process_input() { angle += controller->GetLeftJoystickXAxis() * deltaTime * speed; printf("%f\n", speed); + // printf("%f\n", controller->GetLeftJoystickXAxis()); } int main() {