Fix the abstractions.

Don't place code instructions in the wrong order..
This commit is contained in:
Fries 2024-03-15 17:19:52 -07:00
parent 8f303eec6a
commit eb21ac1dc7
13 changed files with 65 additions and 25 deletions

4
.vscode/launch.json vendored
View file

@ -7,7 +7,7 @@
"program": "${workspaceFolder}/build/src/hello.elf", "program": "${workspaceFolder}/build/src/hello.elf",
"args": [], "args": [],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${fileDirname}", "cwd": "${workspaceFolder}/romdisk",
"environment": [], "environment": [],
"externalConsole": false, "externalConsole": false,
"MIMode": "gdb", "MIMode": "gdb",
@ -31,7 +31,7 @@
"program": "${workspaceFolder}/build/src/gl.elf", "program": "${workspaceFolder}/build/src/gl.elf",
"args": [], "args": [],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${fileDirname}", "cwd": "${workspaceFolder}/romdisk",
"environment": [], "environment": [],
"externalConsole": false, "externalConsole": false,
"MIMode": "gdb", "MIMode": "gdb",

View file

@ -4,24 +4,31 @@
#include <GL/glkos.h> #include <GL/glkos.h>
#include <GL/glu.h> #include <GL/glu.h>
#include <stb_image/stb_image.h> #include <stb_image/stb_image.h>
#define SANIC_LOCATION "/rd/sanic.png"
#define CUBE_LOCATION "/rd/cube.obj"
#else #else
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include <GLFW/glfw3.h>
#include <GL/glu.h> #include <GL/glu.h>
#include <GLFW/glfw3.h>
#include <stb/stb_image.h> #include <stb/stb_image.h>
#define SANIC_LOCATION "./sanic.png"
#define CUBE_LOCATION "./cube.obj"
#endif #endif
#include <tiny_obj_loader.h> #include <tiny_obj_loader.h>
#include <cstdio> #include <cstdio>
#include <stdexcept>
#include <vector> #include <vector>
#include "engine/engine.hh" #include "engine/engine.hh"
class Cube : public Engine { class Cube : public Engine {
unsigned int texture; unsigned int texture;
tinyobj::ObjReader reader; tinyobj::ObjReader reader;
bool parseObj(std::string objFile, tinyobj::ObjReader& reader); bool parseObj(std::string objFile, tinyobj::ObjReader& reader,
char* message);
void cube(); void cube();
void displayStuff(); void displayStuff();
void gameLoop() override { displayStuff(); } void gameLoop() override { displayStuff(); }
@ -40,10 +47,10 @@ void Cube::model() {
for (size_t s = 0; s < shapes.size(); s++) { for (size_t s = 0; s < shapes.size(); s++) {
// Loop over faces(polygon) // Loop over faces(polygon)
size_t index_offset = 0; size_t index_offset = 0;
std::vector<Vector3> vertexes {}; std::vector<Vector3> vertexes{};
std::vector<Vector3> normals {}; std::vector<Vector3> normals{};
std::vector<Vector3> textureCoords {}; std::vector<Vector3> textureCoords{};
for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { 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]); size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]);
@ -62,7 +69,6 @@ void Cube::model() {
vertexes.push_back(vertex); vertexes.push_back(vertex);
// glVertex3f(vx, vy, vz); // glVertex3f(vx, vy, vz);
// drawCalls += 1; // drawCalls += 1;
// Check if `normal_index` is zero or positive. negative = no // Check if `normal_index` is zero or positive. negative = no
// normal data // normal data
@ -107,21 +113,23 @@ void Cube::model() {
glNormalPointer(GL_FLOAT, 0, &normals[0]); glNormalPointer(GL_FLOAT, 0, &normals[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &textureCoords[0]); glTexCoordPointer(2, GL_FLOAT, 0, &textureCoords[0]);
glDrawArrays(GL_TRIANGLES, 0, vertexes.size()); 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; drawCalls += 1;
} }
printf("Draw Calls: %i\n", drawCalls); // printf("Draw Calls: %i\n", drawCalls);
drawCalls = 0; drawCalls = 0;
// glDrawArrays(GL_QUADS, 0, susSize); // 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; tinyobj::ObjReaderConfig readerConfig;
readerConfig.mtl_search_path = "./"; readerConfig.mtl_search_path = "./";
if (!reader.ParseFromFile(objFile, readerConfig)) { if (!reader.ParseFromFile(objFile, readerConfig)) {
if (!reader.Error().empty()) { if (!reader.Error().empty()) {
printf("TinyOBJReader: %s\n", reader.Error().c_str()); sprintf(message, "TinyOBJReader: %s", reader.Error().c_str());
return false; return false;
} }
} }
@ -138,7 +146,6 @@ void Cube::displayStuff() {
} }
void Cube::initScreen() { void Cube::initScreen() {
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
gluPerspective(45.0f, 640.0f / 480.0f, 0.1f, 100.0f); gluPerspective(45.0f, 640.0f / 480.0f, 0.1f, 100.0f);
@ -154,21 +161,23 @@ void Cube::initScreen() {
int width, height, nr_channels; int width, height, nr_channels;
unsigned char* data = unsigned char* data =
stbi_load("/rd/sanic.png", &width, &height, &nr_channels, 0); stbi_load(SANIC_LOCATION, &width, &height, &nr_channels, 0);
if (data) { if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, data); GL_UNSIGNED_BYTE, data);
#ifdef _arch_dreamcast #ifdef _arch_dreamcast
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
#endif #endif
} else { } else {
printf("Texture failed to load.\n"); printf("Texture failed to load.\n");
} }
stbi_image_free(data); 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() { int main() {

View file

@ -5,9 +5,11 @@ DreamcastController::DreamcastController() {
} }
bool DreamcastController::InitializeController() { bool DreamcastController::InitializeController() {
controller = maple_enum_type(0, MAPLE_FUNC_CONTROLLER); maple_device_t* cont = nullptr;
if (this->controller) { cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);
controller_state = (cont_state_t*)maple_dev_status(controller); if (cont->valid) {
this->controller = cont;
this->controller_state = (cont_state_t*)maple_dev_status(controller);
return true; return true;
} else { } else {
controller = nullptr; controller = nullptr;

View file

@ -1,8 +1,18 @@
#include "linuxController.hh" #include "linuxController.hh"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <fstream>
#include <sstream>
bool LinuxController::InitializeController() { 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)) { if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) {
glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState); glfwGetGamepadState(GLFW_JOYSTICK_1, &controllerState);
return true; return true;

View file

@ -21,4 +21,8 @@ void DreamcastEngine::initializeController() {
void DreamcastEngine::SwapBuffers() { void DreamcastEngine::SwapBuffers() {
glKosSwapBuffers(); glKosSwapBuffers();
} }
bool DreamcastEngine::ShouldWindowClose() {
return false;
}
#endif #endif

View file

@ -6,5 +6,6 @@ class DreamcastEngine : public NativeEngine {
void initializeController() override; void initializeController() override;
void initializeEngine() override; void initializeEngine() override;
void SwapBuffers() override; void SwapBuffers() override;
bool ShouldWindowClose() override;
}; };
#endif #endif

View file

@ -20,16 +20,20 @@ void Engine::initializeEngine() {
#endif #endif
engine->initializeEngine(); engine->initializeEngine();
initScreen(); initScreen();
controller = engine->controller;
} }
void Engine::initializeController() { void Engine::initializeController() {
engine->initializeController(); engine->initializeController();
controller = engine->controller;
} }
void Engine::SwapBuffers() { void Engine::SwapBuffers() {
engine->SwapBuffers(); engine->SwapBuffers();
} }
bool Engine::ShouldWindowClose() {
return engine->ShouldWindowClose();
}
void Engine::initScreen() { void Engine::initScreen() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glViewport(0, 0, 640, 480); glViewport(0, 0, 640, 480);
@ -63,7 +67,7 @@ void Engine::deltaTimeLoop(T&& callback, struct timeval& beginningOfFrame,
void Engine::initializeGameLoop() { void Engine::initializeGameLoop() {
struct timeval beginningOfFrame, endOfFrame; struct timeval beginningOfFrame, endOfFrame;
while (1) { while (!ShouldWindowClose()) {
deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame); deltaTimeLoop(&Engine::gameLoop, beginningOfFrame, endOfFrame);
} }
} }

View file

@ -19,13 +19,15 @@ class Engine {
bool thirtyfps; bool thirtyfps;
unsigned int buttonsPressed; unsigned int buttonsPressed;
Controller* controller = nullptr;
virtual void initScreen(); virtual void initScreen();
void printSystemInformation();
virtual void gameLoop(){}; virtual void gameLoop(){};
void printSystemInformation();
void pressButton(Controller::Button button, std::function<void()> callback); void pressButton(Controller::Button button, std::function<void()> callback);
void glVertex3fNormalized(float x, float y, float z); void glVertex3fNormalized(float x, float y, float z);
void SwapBuffers(); void SwapBuffers();
Controller* controller = nullptr; bool ShouldWindowClose();
private: private:
template <typename T> template <typename T>

View file

@ -41,4 +41,8 @@ void LinuxEngine::SwapBuffers() {
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
bool LinuxEngine::ShouldWindowClose() {
return glfwWindowShouldClose(this->window);
}
#endif #endif

View file

@ -8,6 +8,7 @@ class LinuxEngine : public NativeEngine {
void initializeController() override; void initializeController() override;
void initializeEngine() override; void initializeEngine() override;
void SwapBuffers() override; void SwapBuffers() override;
bool ShouldWindowClose() override;
GLFWwindow* window = nullptr; GLFWwindow* window = nullptr;
}; };
#endif #endif

View file

@ -6,6 +6,7 @@ class NativeEngine {
virtual void initializeController(){}; virtual void initializeController(){};
virtual void initializeEngine(){}; virtual void initializeEngine(){};
virtual void SwapBuffers(){}; virtual void SwapBuffers(){};
virtual bool ShouldWindowClose(){return false;};
Controller* controller = nullptr; Controller* controller = nullptr;
}; };
#endif #endif

View file

@ -55,6 +55,7 @@ void Glcc::displayStuff() {
} }
void Glcc::process_input() { void Glcc::process_input() {
controller->PollController();
pressButton(Controller::Button::DPAD_LEFT, [&]() { speed -= 1.0f; }); pressButton(Controller::Button::DPAD_LEFT, [&]() { speed -= 1.0f; });
pressButton(Controller::Button::DPAD_RIGHT, [&]() { speed += 1.0f; }); pressButton(Controller::Button::DPAD_RIGHT, [&]() { speed += 1.0f; });
pressButton(Controller::Button::A, [&]() { thirtyfps = !thirtyfps; }); pressButton(Controller::Button::A, [&]() { thirtyfps = !thirtyfps; });
@ -63,6 +64,7 @@ void Glcc::process_input() {
angle += controller->GetLeftJoystickXAxis() * deltaTime * speed; angle += controller->GetLeftJoystickXAxis() * deltaTime * speed;
printf("%f\n", speed); printf("%f\n", speed);
// printf("%f\n", controller->GetLeftJoystickXAxis());
} }
int main() { int main() {