Compiling again.
This commit is contained in:
@ -19,7 +19,7 @@ target_link_libraries(${PROJECT_NAME}
|
||||
|
||||
target_sources(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
glfwclient.cpp
|
||||
glfwclient.c
|
||||
)
|
||||
|
||||
# Includes
|
||||
|
149
client/glfwclient/glfwclient.c
Normal file
149
client/glfwclient/glfwclient.c
Normal file
@ -0,0 +1,149 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "glfwclient.h"
|
||||
|
||||
static game_t *GAME_STATE;
|
||||
static GLFWwindow *window = NULL;
|
||||
|
||||
int32_t main() {
|
||||
double time, newTime;
|
||||
game_t *game;
|
||||
input_t *input;
|
||||
float fDelta;
|
||||
|
||||
// Attempt to init GLFW
|
||||
if(!glfwInit()) return 1;
|
||||
|
||||
// Setup window hints
|
||||
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, false);
|
||||
|
||||
// Create Window
|
||||
window = glfwCreateWindow(
|
||||
WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT, "", NULL, NULL
|
||||
);
|
||||
if(!window) {
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Load GLAD
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSwapInterval(0);
|
||||
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
|
||||
// Setup window listeners
|
||||
glfwSetWindowSizeCallback(window, &glfwOnResize);
|
||||
glfwSetKeyCallback(window, &glfwOnKey);
|
||||
glfwSetErrorCallback(&glfwOnError);
|
||||
glfwSetCursorPosCallback(window, &glfwOnCursor);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
|
||||
// Prepare the game
|
||||
game = malloc(sizeof(game_t));
|
||||
GAME_STATE = game;
|
||||
input = &game->engine.input;
|
||||
|
||||
printf("Game is %zu bytes.\n", sizeof(game_t));
|
||||
|
||||
// Init the render resolution
|
||||
renderSetResolution(&game->engine.render,
|
||||
WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT
|
||||
);
|
||||
// Init the game
|
||||
if(gameInit(game)) {
|
||||
// Bind initial keys
|
||||
inputBind(input, INPUT_NULL, glfwGetInputSourceForKey(GLFW_KEY_ESCAPE));
|
||||
inputBind(input, INPUT_UP, glfwGetInputSourceForKey(GLFW_KEY_UP));
|
||||
inputBind(input, INPUT_DOWN, glfwGetInputSourceForKey(GLFW_KEY_DOWN));
|
||||
inputBind(input, INPUT_LEFT, glfwGetInputSourceForKey(GLFW_KEY_LEFT));
|
||||
inputBind(input, INPUT_RIGHT, glfwGetInputSourceForKey(GLFW_KEY_RIGHT));
|
||||
inputBind(input, INPUT_UP, glfwGetInputSourceForKey(GLFW_KEY_W));
|
||||
inputBind(input, INPUT_DOWN, glfwGetInputSourceForKey(GLFW_KEY_S));
|
||||
inputBind(input, INPUT_LEFT, glfwGetInputSourceForKey(GLFW_KEY_A));
|
||||
inputBind(input, INPUT_RIGHT, glfwGetInputSourceForKey(GLFW_KEY_D));
|
||||
inputBind(input, INPUT_ACCEPT, glfwGetInputSourceForKey(GLFW_KEY_E));
|
||||
inputBind(input, INPUT_ACCEPT, glfwGetInputSourceForKey(GLFW_KEY_ENTER));
|
||||
inputBind(input, INPUT_ACCEPT, glfwGetInputSourceForKey(GLFW_KEY_SPACE));
|
||||
|
||||
// Bind the fake inputs
|
||||
inputBind(input, INPUT_MOUSE_X, GLFW_PLATFORM_INPUT_MOUSE_X);
|
||||
inputBind(input, INPUT_MOUSE_Y, GLFW_PLATFORM_INPUT_MOUSE_Y);
|
||||
|
||||
// Set up the client
|
||||
game->engine.client.setTitle = &glfwClientSetTitle;
|
||||
|
||||
// Set up some GLFW stuff
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
glfwSetWindowTitle(window, game->engine.name);
|
||||
|
||||
// Begin time.
|
||||
time = 0;
|
||||
|
||||
// Main Render Loop
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
glfwPollEvents();
|
||||
|
||||
// Determine the delta.
|
||||
newTime = glfwGetTime();
|
||||
fDelta = (float)(newTime - time);
|
||||
time = newTime;
|
||||
|
||||
// Tick the engine.
|
||||
if(!gameUpdate(game, fDelta)) break;
|
||||
glfwSwapBuffers(window);
|
||||
sleep(0);//Fixes some weird high CPU bug, not actually necessary.
|
||||
}
|
||||
|
||||
|
||||
// Game has finished running, cleanup.
|
||||
gameDispose(game);
|
||||
}
|
||||
|
||||
free(game);
|
||||
|
||||
// Terminate the GLFW context.
|
||||
glfwSetWindowSizeCallback(window, NULL);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) {
|
||||
renderSetResolution(&GAME_STATE->engine.render, (float)width, (float)height);
|
||||
}
|
||||
|
||||
void glfwOnKey(GLFWwindow *window,
|
||||
int32_t key, int32_t scancode, int32_t action, int32_t mods
|
||||
) {
|
||||
input_t *input = &GAME_STATE->engine.input;
|
||||
if(action == GLFW_PRESS) {
|
||||
inputStateSet(input, glfwGetInputSourceForKey(key), 1.0f);
|
||||
} else if(action == GLFW_RELEASE) {
|
||||
inputStateSet(input, glfwGetInputSourceForKey(key), 0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void glfwOnError(int error, const char* description) {
|
||||
fputs(description, stderr);
|
||||
}
|
||||
|
||||
void glfwOnCursor(GLFWwindow *window, double x, double y) {
|
||||
input_t *input = &GAME_STATE->engine.input;
|
||||
inputStateSet(input, GLFW_PLATFORM_INPUT_MOUSE_X, (float)x);
|
||||
inputStateSet(input, GLFW_PLATFORM_INPUT_MOUSE_Y, (float)y);
|
||||
}
|
||||
|
||||
inputsource_t glfwGetInputSourceForKey(int32_t key) {
|
||||
return (inputsource_t)((
|
||||
key <= GLFW_KEY_GRAVE_ACCENT ? key - GLFW_KEY_SPACE :
|
||||
key <= GLFW_KEY_MENU ? key - GLFW_KEY_ESCAPE + GLFW_KEY_GRAVE_ACCENT :
|
||||
key
|
||||
) % INPUT_SOURCE_COUNT);
|
||||
}
|
||||
|
||||
void glfwClientSetTitle(char *name) {
|
||||
glfwSetWindowTitle(window, name);
|
||||
}
|
@ -1,157 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "glfwclient.hpp"
|
||||
|
||||
static GLFWEngine *engine;
|
||||
|
||||
GLFWEngine::GLFWEngine(char **args, int32_t argc) {
|
||||
this->args = args;
|
||||
this->argc = argc;
|
||||
this->window = NULL;
|
||||
}
|
||||
|
||||
int32_t GLFWEngine::init(void) {
|
||||
if(!glfwInit()) return 1;
|
||||
|
||||
// Setup window hints
|
||||
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, false);
|
||||
|
||||
// Create Window
|
||||
this->window = glfwCreateWindow(
|
||||
WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT, this->game.name, NULL, NULL
|
||||
);
|
||||
if(!this->window) {
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Load GLAD
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSwapInterval(0);
|
||||
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
|
||||
// Init the game
|
||||
this->game.init();
|
||||
|
||||
// Init the render resolution
|
||||
this->game.render.setResolution(WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT);
|
||||
|
||||
// Bind initial keys
|
||||
this->game.input.bind(glfwGetInputSourceForKey(GLFW_KEY_ESCAPE), INPUT_NULL);
|
||||
// input = &this->game.input;
|
||||
// inputBind(input, INPUT_NULL, glfwGetInputSourceForKey(GLFW_KEY_ESCAPE));
|
||||
// inputBind(input, INPUT_UP, glfwGetInputSourceForKey(GLFW_KEY_UP));
|
||||
// inputBind(input, INPUT_DOWN, glfwGetInputSourceForKey(GLFW_KEY_DOWN));
|
||||
// inputBind(input, INPUT_LEFT, glfwGetInputSourceForKey(GLFW_KEY_LEFT));
|
||||
// inputBind(input, INPUT_RIGHT, glfwGetInputSourceForKey(GLFW_KEY_RIGHT));
|
||||
// inputBind(input, INPUT_UP, glfwGetInputSourceForKey(GLFW_KEY_W));
|
||||
// inputBind(input, INPUT_DOWN, glfwGetInputSourceForKey(GLFW_KEY_S));
|
||||
// inputBind(input, INPUT_LEFT, glfwGetInputSourceForKey(GLFW_KEY_A));
|
||||
// inputBind(input, INPUT_RIGHT, glfwGetInputSourceForKey(GLFW_KEY_D));
|
||||
// inputBind(input, INPUT_ACCEPT, glfwGetInputSourceForKey(GLFW_KEY_E));
|
||||
// inputBind(input, INPUT_ACCEPT, glfwGetInputSourceForKey(GLFW_KEY_ENTER));
|
||||
// inputBind(input, INPUT_ACCEPT, glfwGetInputSourceForKey(GLFW_KEY_SPACE));
|
||||
|
||||
// Bind the fake inputs
|
||||
this->game.input.bind(GLFW_PLATFORM_INPUT_MOUSE_X, INPUT_MOUSE_X);
|
||||
this->game.input.bind(GLFW_PLATFORM_INPUT_MOUSE_Y, INPUT_MOUSE_Y);
|
||||
|
||||
// Set up some GLFW callbacks
|
||||
glfwSetWindowSizeCallback(window, &glfwOnResize);
|
||||
glfwSetKeyCallback(window, &glfwOnKey);
|
||||
glfwSetErrorCallback(&glfwOnError);
|
||||
glfwSetCursorPosCallback(window, &glfwOnCursor);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
// glfwSetWindowTitle(window, game->engine.name);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t GLFWEngine::update(float delta) {
|
||||
int32_t ret;
|
||||
|
||||
ret = this->game.update(delta);
|
||||
if(ret != 0) return ret;
|
||||
|
||||
if(this->game.input.isPressed(INPUT_NULL)) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
GLFWEngine::~GLFWEngine(void) {
|
||||
glfwSetWindowSizeCallback(window, NULL);
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
|
||||
int32_t main(int32_t argc, char **args) {
|
||||
int32_t ret;
|
||||
double time, newTime;
|
||||
float fDelta;
|
||||
|
||||
// Init engine
|
||||
engine = new GLFWEngine(args, argc);
|
||||
ret = engine->init();
|
||||
if(ret != 0) return ret;
|
||||
|
||||
printf("Game is %zu bytes.\n", sizeof(engine));
|
||||
|
||||
// Main Render Loop
|
||||
while(!glfwWindowShouldClose(engine->window)) {
|
||||
glfwPollEvents();
|
||||
|
||||
// Determine the delta.
|
||||
newTime = glfwGetTime();
|
||||
fDelta = (float)(newTime - time);
|
||||
time = newTime;
|
||||
|
||||
ret = engine->update(fDelta);
|
||||
|
||||
// Tick the engine.
|
||||
glfwSwapBuffers(engine->window);
|
||||
sleep(0);
|
||||
|
||||
if(ret != 0) break;
|
||||
}
|
||||
|
||||
// Cleanup the engine.
|
||||
delete engine;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) {
|
||||
engine->game.render.setResolution((float)width, (float)height);
|
||||
}
|
||||
|
||||
void glfwOnKey(GLFWwindow *window,
|
||||
int32_t key, int32_t scancode, int32_t action, int32_t mods
|
||||
) {
|
||||
if(action == GLFW_PRESS) {
|
||||
engine->game.input.setValue(glfwGetInputSourceForKey(key), 1.0f);
|
||||
} else if(action == GLFW_RELEASE) {
|
||||
engine->game.input.setValue(glfwGetInputSourceForKey(key), 0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void glfwOnError(int error, const char* description) {
|
||||
fputs(description, stderr);
|
||||
}
|
||||
|
||||
void glfwOnCursor(GLFWwindow *window, double x, double y) {
|
||||
engine->game.input.setValue(GLFW_PLATFORM_INPUT_MOUSE_X, (float)x);
|
||||
engine->game.input.setValue(GLFW_PLATFORM_INPUT_MOUSE_Y, (float)y);
|
||||
}
|
||||
|
||||
inputsource_t glfwGetInputSourceForKey(int32_t key) {
|
||||
return (inputsource_t)((
|
||||
key <= GLFW_KEY_GRAVE_ACCENT ? key - GLFW_KEY_SPACE :
|
||||
key <= GLFW_KEY_MENU ? key - GLFW_KEY_ESCAPE + GLFW_KEY_GRAVE_ACCENT :
|
||||
key
|
||||
) % 0xFF);
|
||||
}
|
58
client/glfwclient/glfwclient.h
Normal file
58
client/glfwclient/glfwclient.h
Normal file
@ -0,0 +1,58 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <libs.h>
|
||||
#include "display/render.h"
|
||||
#include "input/input.h"
|
||||
#include "game/game.h"
|
||||
|
||||
#define WINDOW_WIDTH_DEFAULT 1280
|
||||
#define WINDOW_HEIGHT_DEFAULT WINDOW_WIDTH_DEFAULT/16*9
|
||||
|
||||
#define GLFW_PLATFORM_INPUT_MOUSE_X (inputsource_t)0xFF
|
||||
#define GLFW_PLATFORM_INPUT_MOUSE_Y (inputsource_t)0xFE
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
|
||||
|
||||
void glfwOnError(int error, const char* description);
|
||||
void glfwOnCursor(GLFWwindow *window, double x, double y);
|
||||
|
||||
|
||||
/**
|
||||
* Get the game engine specific input source for a given GLFW Key code.
|
||||
*
|
||||
* @param key Key to get the input source for.
|
||||
* @return The input source.
|
||||
*/
|
||||
inputsource_t glfwGetInputSourceForKey(int32_t key);
|
||||
|
||||
/** GLFW Client Methods */
|
||||
void glfwClientSetTitle(char *name);
|
@ -1,105 +0,0 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
extern "C" {
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "libs.h"
|
||||
}
|
||||
|
||||
#define WINDOW_WIDTH_DEFAULT 1280
|
||||
#define WINDOW_HEIGHT_DEFAULT WINDOW_WIDTH_DEFAULT/16*9
|
||||
|
||||
#define GLFW_PLATFORM_INPUT_MOUSE_X (inputsource_t)0xFF
|
||||
#define GLFW_PLATFORM_INPUT_MOUSE_Y (inputsource_t)0xFE
|
||||
|
||||
class GLFWEngine {
|
||||
private:
|
||||
char **args;
|
||||
int32_t argc;
|
||||
|
||||
public:
|
||||
GLFWwindow *window;
|
||||
|
||||
/**
|
||||
* Construct a new GLFW Engine instance.
|
||||
*
|
||||
* @param argc Count of arguments.
|
||||
* @param args Array of strings for the arguments provided to the engine.
|
||||
*/
|
||||
GLFWEngine(int32_t argc, char **args);
|
||||
|
||||
/**
|
||||
* Initializes the GLFW engine.
|
||||
*
|
||||
* @return 0 if success, everything else is a failure.
|
||||
*/
|
||||
virtual int32_t init(void);
|
||||
|
||||
/**
|
||||
* Tick the game engine by delta amount.
|
||||
*
|
||||
* @param delta
|
||||
* @return 0 if success, everything else is a failure.
|
||||
*/
|
||||
int32_t update(float delta);
|
||||
|
||||
virtual ~GLFWEngine(void) override;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Entry of the program
|
||||
*
|
||||
* @param argc Count of args in the args array.
|
||||
* @param args Args provided to us by the parent O.S.
|
||||
* @return 0 if success, anything else for failure.
|
||||
*/
|
||||
int32_t main(int32_t argc, char **args);
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
|
||||
/**
|
||||
* Callback for generic GLFW errors.
|
||||
*
|
||||
* @param error Error code/descriptor.
|
||||
* @param description String representation of the error.
|
||||
*/
|
||||
void glfwOnError(int error, const char* description);
|
||||
|
||||
/**
|
||||
* Event callback for mouse cursor movement.
|
||||
*
|
||||
* @param window
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
void glfwOnCursor(GLFWwindow *window, double x, double y);
|
||||
|
||||
|
||||
/**
|
||||
* Get the game engine specific input source for a given GLFW Key code.
|
||||
*
|
||||
* @param key Key to get the input source for.
|
||||
* @return The input source.
|
||||
*/
|
||||
inputsource_t glfwGetInputSourceForKey(int32_t key);
|
Reference in New Issue
Block a user