This commit is contained in:
2026-07-02 21:59:55 -07:00
commit e14550ce63
12 changed files with 370 additions and 0 deletions

75
source/engin/window.c Normal file
View File

@@ -0,0 +1,75 @@
#include "window.h"
#include "GLFW/glfw3.h"
#include <string.h>
int keyState[GLFW_KEY_LAST] = {0};
// Key name lookup table (optional, you can hardcode or load from a config)
const char* keyNames[GLFW_KEY_LAST] = {
"Escape", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
"Backspace", "Tab", "Return", "Space", "Delete", "Insert", "Home", "End", "PageUp", "PageDown",
"Left", "Up", "Right", "Down", "PrintScreen", "Pause", "ScrollLock", "NumLock", "CapsLock",
"Shift", "Control", "Alt", "Super", "Enter", "Backspace", "Tab", "Space",
// Add more key names as needed
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
// Add more if needed
};
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
int WindowCreate(window* w, window_config *config) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
w->ctx = glfwCreateWindow(config->width, config->height, config->Name, 0, 0);
glfwMakeContextCurrent(w->ctx);
glfwSetKeyCallback(w->ctx, keyCallback);
glewExperimental = GL_TRUE;
glewInit();
glfwGetFramebufferSize(w->ctx, &w->width, &w->height);
return 0;
}
int WindowHandleInput(window* w) {
return 0;
}
int WindowEventHandler(window* w) {
return 0;
}
static int keyNameToCode(const char* name) {
for (int i = 0; i < GLFW_KEY_LAST; ++i) {
if (strcmp(name, keyNames[i]) == 0) {
return i;
}
}
return -1; // invalid key
}
// GetKey wrapper
int GetKey(const char* keyName) {
int keyCode = keyNameToCode(keyName);
if (keyCode == -1) {
return 0; // or you can handle error differently
}
return keyState[keyCode];
}
// GLFW key callback
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key >= 0 && key < GLFW_KEY_LAST) {
if (action == GLFW_PRESS) {
keyState[key] = 1;
} else if (action == GLFW_RELEASE) {
keyState[key] = 0;
}
}
}