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",
"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",

View file

@ -4,24 +4,31 @@
#include <GL/glkos.h>
#include <GL/glu.h>
#include <stb_image/stb_image.h>
#define SANIC_LOCATION "/rd/sanic.png"
#define CUBE_LOCATION "/rd/cube.obj"
#else
#define STB_IMAGE_IMPLEMENTATION
#include <GLFW/glfw3.h>
#include <GL/glu.h>
#include <GLFW/glfw3.h>
#include <stb/stb_image.h>
#define SANIC_LOCATION "./sanic.png"
#define CUBE_LOCATION "./cube.obj"
#endif
#include <tiny_obj_loader.h>
#include <cstdio>
#include <stdexcept>
#include <vector>
#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(); }
@ -63,7 +70,6 @@ void Cube::model() {
// glVertex3f(vx, vy, vz);
// drawCalls += 1;
// Check if `normal_index` is zero or positive. negative = no
// normal data
if (idx.normal_index >= 0) {
@ -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,7 +161,7 @@ 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,
@ -168,7 +175,9 @@ void Cube::initScreen() {
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() {

View file

@ -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;

View file

@ -1,8 +1,18 @@
#include "linuxController.hh"
#include <GLFW/glfw3.h>
#include <fstream>
#include <sstream>
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;

View file

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

View file

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

View file

@ -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);
}
}

View file

@ -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<void()> callback);
void glVertex3fNormalized(float x, float y, float z);
void SwapBuffers();
Controller* controller = nullptr;
bool ShouldWindowClose();
private:
template <typename T>

View file

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

View file

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

View file

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

View file

@ -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() {