Much much better
This commit is contained in:
75
src/platform/glfw/glwfwplatform.c
Normal file
75
src/platform/glfw/glwfwplatform.c
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "glwfwplatform.h"
|
||||
|
||||
GLFWwindow *window;
|
||||
game_t *runningGame;
|
||||
|
||||
int32_t main() {
|
||||
// Create out platform context
|
||||
platform_t platform = {
|
||||
.name = "glfw",
|
||||
.screenWidth = WINDOW_WIDTH_DEFAULT,
|
||||
.screenHeight = WINDOW_HEIGHT_DEFAULT
|
||||
};
|
||||
|
||||
// Attempt to init GLFW
|
||||
if(!glfwInit()) return NULL;
|
||||
window = glfwCreateWindow(platform.screenWidth, platform.screenHeight,
|
||||
"", NULL, NULL
|
||||
);
|
||||
if(!window) {
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Load GLAD
|
||||
glfwMakeContextCurrent(window);
|
||||
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
|
||||
// Setup window listeners
|
||||
glfwSetWindowSizeCallback(window, &glfwOnResize);
|
||||
glfwSetKeyCallback(window, &glfwOnKey);
|
||||
|
||||
// Create the game instance
|
||||
runningGame = gameInit(&platform);
|
||||
if(runningGame == NULL) return 1;
|
||||
|
||||
// Main Render Loop
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
gameUpdate(runningGame);
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// Game has finished running, cleanup.
|
||||
gameDispose(runningGame);
|
||||
|
||||
// Terminate the GLFW context.
|
||||
glfwSetWindowSizeCallback(window, NULL);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) {
|
||||
engine_t *engine = gameGetEngine(runningGame);
|
||||
engine->platform->screenWidth = width;
|
||||
engine->platform->screenWidth = height;
|
||||
renderSetResolution(engine->render, width, height);
|
||||
}
|
||||
|
||||
void glfwOnKey(GLFWwindow *window,
|
||||
int32_t key, int32_t scancode, int32_t action, int32_t mods
|
||||
) {
|
||||
char *title = glfwGetKeyName(key, scancode);
|
||||
if(title == NULL) {
|
||||
printf("Unknown Key %d", scancode);
|
||||
} else {
|
||||
printf(title);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
47
src/platform/glfw/glwfwplatform.h
Normal file
47
src/platform/glfw/glwfwplatform.h
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
// I load GLAD and GLFW Here because they need to be included in specific orders
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stdint.h>
|
||||
#include "../../engine/platform.h"
|
||||
#include "../../engine/game/game.h"
|
||||
#include "../../engine/display/render.h"
|
||||
|
||||
#define WINDOW_WIDTH_DEFAULT 480
|
||||
#define WINDOW_HEIGHT_DEFAULT 270
|
||||
|
||||
/** The GLFW Window Context Pointer */
|
||||
extern GLFWwindow *window;
|
||||
extern game_t *runningGame;
|
||||
|
||||
/**
|
||||
* Entry of the program
|
||||
* @return 0 if success, anything else for failure.
|
||||
*/
|
||||
int32_t main();
|
||||
|
||||
|
||||
/**
|
||||
* Resize callbacks.
|
||||
*
|
||||
* @param window Window that was resized.
|
||||
* @param width New window width.
|
||||
* @param height New window height.
|
||||
*/
|
||||
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height);
|
||||
|
||||
|
||||
/**
|
||||
* Keyboard Input callbacks.
|
||||
*
|
||||
* @param window Window that was resized.
|
||||
*/
|
||||
void glfwOnKey(GLFWwindow *window,
|
||||
int32_t key, int32_t scancode, int32_t action, int32_t mods
|
||||
);
|
@ -1,52 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include <malloc.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void platforminputsource_t;
|
||||
|
||||
/**
|
||||
* Platform stuct, contains information about the device/method that is running
|
||||
* the program that the game itself is running on, typically this will be either
|
||||
* the operating system, or perhaps the specific device information.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Internal name of the platform */
|
||||
char *name;
|
||||
|
||||
/** List of known input sources */
|
||||
platforminputsource_t **inputSources;
|
||||
/** Count of known input source */
|
||||
uint32_t *inputSourceCount;
|
||||
/** State of the input sources, updated by the platform. */
|
||||
float *inputStates;
|
||||
} platform_t;
|
||||
|
||||
/**
|
||||
* Initialize the current platform context. The platform itself is responsible
|
||||
* for deciding how this method is implemented.
|
||||
*
|
||||
* @return The context information for the platform.
|
||||
*/
|
||||
platform_t * platformInit();
|
||||
|
||||
/**
|
||||
* Cleanup a previously created platform instance.
|
||||
*
|
||||
* @param platform The platform instance to cleanse.
|
||||
*/
|
||||
void platformDispose(platform_t *platform);
|
||||
|
||||
/**
|
||||
* Method will be polled for each known input source to request the current
|
||||
* state from the device. Values returned will be defined within the 0 to 1
|
||||
* range.
|
||||
*
|
||||
* @param source The input source.
|
||||
* @return Input state between 0 and 1.
|
||||
*/
|
||||
float platformInputGetState(platforminputsource_t *source);
|
@ -1,26 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "win32platform.h"
|
||||
|
||||
platform_t * platformInit() {
|
||||
platform_t *platform = malloc(sizeof(platform_t));
|
||||
|
||||
platform->name = WIN32_NAME;
|
||||
|
||||
platform->inputSourceCount = 0;
|
||||
platform->inputSources = NULL;
|
||||
platform->inputStates = NULL;
|
||||
|
||||
return platform;
|
||||
}
|
||||
|
||||
void platformDispose(platform_t *platform) {
|
||||
free(platform);
|
||||
}
|
||||
|
||||
float platformInputGetState(platforminputsource_t *source) {
|
||||
return 0;
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../platform.h"
|
||||
|
||||
#define WIN32_NAME "Win32"
|
Reference in New Issue
Block a user