Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,41 @@ void game_run()

CreateThings();

double delta_time = 1000.0 / 100.0f;
std::chrono::system_clock::time_point last_frame = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> work_time;

std::chrono::system_clock::time_point last_print = std::chrono::system_clock::now();
std::chrono::system_clock::time_point current_time = std::chrono::system_clock::now();
std::chrono::duration<float, std::milli> since_last_print;

int frames = 0;

while (s_running)
{
last_frame = std::chrono::system_clock::now();

renderer_prepare();
renderer_render();
renderer_present();

// HACK: Delta time should be calculated correctly
std::this_thread::sleep_for(std::chrono::duration<float, std::milli>(16));
frames++;

since_last_print = current_time - last_print;

if (since_last_print.count() > 1000.0f)
{
last_print = std::chrono::system_clock::now();
std::cout << "frames: " << frames << std::endl;
frames = 0;
}

current_time = std::chrono::system_clock::now();
work_time = current_time - last_frame;

std::chrono::duration<double, std::milli> delta_ms(delta_time - work_time.count());
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(delta_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count()));
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/keyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "keyboard.h"

static keyboard_data s_data;

void keyboard_init(GLFWwindow* window)
{
glfwSetKeyCallback(window, keyboard_callback);
}

bool keyboard_is_pressed(int key)
{
return s_data.keys[key];
}

void keyboard_state_set(int key, bool state)
{
s_data.keys[key] = state;
}

void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS)
{
keyboard_state_set(key, true);
}

else if (action == GLFW_RELEASE)
{
keyboard_state_set(key, false);
}
}
16 changes: 16 additions & 0 deletions src/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <GLFW/glfw3.h>
#include <bitset>

struct keyboard_data

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PascalCase för structs är conventions som jag har jobbat med

{
GLFWwindow* window;
std::bitset<1024> keys;
};

void keyboard_init(GLFWwindow* window);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase för funktioner

bool keyboard_is_pressed(int key);
void keyboard_state_set(int key, bool state);

void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
52 changes: 52 additions & 0 deletions src/mouse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "mouse.h"

static mouse_data s_data;

void mouse_init(GLFWwindow* window)
{
s_data.window = window;
glfwSetInputMode(s_data.window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
s_data.position_x = 0.0f;
s_data.position_y = 0.0f;
}

float mouse_position_get_x()
{
double x, y;
glfwGetCursorPos(s_data.window, &x, &y);

return (float)x;
}

float mouse_position_get_y()
{
double x, y;
glfwGetCursorPos(s_data.window, &x, &y);

return (float)y;
}

void mouse_position_set(float x, float y)
{
s_data.position_x = x;
s_data.position_y = y;
glfwSetCursorPos(s_data.window, (double)x, (double)y);
}

bool mouse_is_left()
{
int state = glfwGetMouseButton(s_data.window, GLFW_MOUSE_BUTTON_LEFT);
return state == GLFW_PRESS;
}

bool mouse_is_right()
{
int state = glfwGetMouseButton(s_data.window, GLFW_MOUSE_BUTTON_RIGHT);
return state == GLFW_PRESS;
}

bool mouse_is_middle()
{
int state = glfwGetMouseButton(s_data.window, GLFW_MOUSE_BUTTON_MIDDLE);
return state == GLFW_PRESS;
}
20 changes: 20 additions & 0 deletions src/mouse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <GLFW/glfw3.h>

struct mouse_data
{
GLFWwindow* window;
float position_x;
float position_y;
};

void mouse_init(GLFWwindow* window);
float mouse_position_get_x();
float mouse_position_get_y();

void mouse_position_set(float x, float y);

bool mouse_is_left();
bool mouse_is_right();
bool mouse_is_middle();
44 changes: 39 additions & 5 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "shader.h"
#include "game.h"
#include "keyboard.h"
#include "mouse.h"

#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
Expand Down Expand Up @@ -38,7 +40,22 @@ bool renderer_init()
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

s_window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Rayman", NULL, NULL);
int monitor_count = 0;
GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
printf("\nMonitor count: %d\n", monitor_count);
const GLFWvidmode* mode = glfwGetVideoMode(monitors[0]);

glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);

printf("Color format: %d bit RGB\n\n", mode->redBits + mode->greenBits + mode->blueBits);
printf("Refresh rate: %d Hz\n\n", mode->refreshRate);

s_window = glfwCreateWindow(mode->width, mode->height, "Rayman", monitors[0], NULL);

//s_window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Rayman", NULL, NULL);


if (!s_window)
Expand All @@ -61,20 +78,28 @@ bool renderer_init()

std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;

keyboard_init(s_window);
mouse_init(s_window);
mouse_position_set(256.0f, 256.0f);

return true;
}

static GLint cam_pos_location = 0;
static GLint target_pos_location = 0;
static GLint power_location = 0;

void CreateThings()
{
unsigned int va;
unsigned int vb;
unsigned int ib;

float verts[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f
};

unsigned int indices[6] = {
Expand All @@ -92,6 +117,10 @@ void CreateThings()

GLuint program = CreateProgram(vertexShaderSource, fragmentShaderSource);
glUseProgram(program);

cam_pos_location = glGetUniformLocation(program, "camera_position");
target_pos_location = glGetUniformLocation(program, "target_position");
power_location = glGetUniformLocation(program, "fractal_power");
}

void renderer_prepare()
Expand All @@ -107,6 +136,11 @@ void renderer_render()
game_close();
}

else if (keyboard_is_pressed(GLFW_KEY_ESCAPE))
{
game_close();
}

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ void GenVertexArray(unsigned int *id);
void DeleteVertexArray(unsigned int *id);
void GenIndexBuffer(unsigned int *id, const unsigned int *data, int count);
void DeleteIndexBuffer(unsigned int *id);
void ConfigVertexArrrayLayout(unsigned int *va, unsigned int *vb, const Layout &layout);
void ConfigVertexArrayLayout(unsigned int *va, unsigned int *vb, const Layout &layout);
void AddToLayout(Layout &layout, GLuint type, unsigned int count, bool normalize = false);
2 changes: 2 additions & 0 deletions src/shader.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <string>

std::string ReadFile(const char *path);
GLuint CreateProgram(const char* VertexPath, const char* FragPath);
void DeleteProgram(GLuint *id);