Learnt git basics. Added changes since first commit. This is essentially the second commit

dev
Shariq Shah 10 years ago
parent 01c61e5030
commit 22e319e391
  1. 6
      src/array.c
  2. 31
      src/game.c
  3. 139
      src/input.c
  4. 26
      src/input.h
  5. 5
      src/renderer.c
  6. 1
      src/renderer.h
  7. 6
      src/window_system.c

@ -98,15 +98,15 @@ bool array_remove_at(Array* array, unsigned int index)
bool success = false;
if(array->length > 0)
{
unsigned int next_index = index++;
unsigned int next_index = index + 1;
if(next_index < array->length)
{
array->length--;
char* current_location = array->data + (array->object_size * index);
char* location_after_obj = current_location + array->object_size;
memmove(current_location,
location_after_obj,
array->object_size * ((array->length - 1) - next_index));
array->object_size * (array->length - next_index));
array->length--;
success = array_reallocate(array);
}
else

@ -15,6 +15,14 @@ void game_init(void)
GLFWwindow* window = window_get_active();
input_init(window);
renderer_init(window);
int keys[2] = {GLFW_KEY_W, GLFW_KEY_UP};
int keys2[2] = {GLFW_KEY_S, GLFW_KEY_DOWN};
int keys3[1] = {GLFW_KEY_J};
int keys4[1] = {GLFW_KEY_K};
input_map_create("MoveUp", keys, 2);
input_map_create("MoveDown", keys2, 2);
input_map_create("Test", keys3, 1);
input_map_create("Test2", keys4, 1);
run();
}
@ -31,11 +39,26 @@ void run(void)
void update(void)
{
if(input_get_key_state(GLFW_KEY_A, GLFW_REPEAT))
log_message("A released");
if(input_map_state_get("MoveUp", GLFW_RELEASE))
log_message("MoveUp pressed!");
if(input_get_mousebutton_state(GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS))
log_message("Left mouse released");
if(input_map_state_get("MoveDown", GLFW_RELEASE))
{
input_map_remvove("Test");
log_message("MoveDown pressed!");
}
if(input_map_state_get("Test", GLFW_RELEASE))
log_message("Test released");
if(input_map_state_get("Test2", GLFW_RELEASE))
{
int keys[] = {GLFW_KEY_R};
input_map_keys_set("Test2", keys, 1);
log_message("Test2 released!");
}
input_update();
}
void render(void)

@ -1,13 +1,21 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "input.h"
#include "GLFW/glfw3.h"
#include "window_system.h"
#include "log.h"
void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods);
void input_on_mousebutton(GLFWwindow* window, int button, int action, int mods);
void input_on_cursor_move(GLFWwindow* window, double xpos, double ypos);
#define KS_INACTIVE -1; /* state for input map is set to KS_INACTIVE(KeyState_Inactive) when
the key is neither pressed nor released */
static void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods);
static void input_on_mousebutton(GLFWwindow* window, int button, int action, int mods);
static void input_on_cursor_move(GLFWwindow* window, double xpos, double ypos);
static int map_find(const char* name);
static Array* input_map_list;
void input_init(GLFWwindow* window)
{
@ -15,14 +23,21 @@ void input_init(GLFWwindow* window)
glfwSetKeyCallback(window, input_on_key);
glfwSetCursorPosCallback(window, input_on_cursor_move);
glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
input_map_list = array_new(Input_Map);
}
void input_cleanup(void)
{
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
array_free(map->keys);
}
array_free(input_map_list);
}
void input_on_cursor_move(GLFWwindow* window, double xpos, double ypos)
static void input_on_cursor_move(GLFWwindow* window, double xpos, double ypos)
{
}
@ -34,17 +49,29 @@ void input_get_cursor_pos(double* xpos, double* ypos)
glfwGetCursorPos(window, xpos, ypos);
}
void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods)
static void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods)
{
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
for(unsigned int j = 0; j < map->keys->length; j++)
{
int map_key = array_get_val(map->keys, int, j);
if(map_key == key)
{
map->state = action;
break;
}
}
}
}
void input_on_mousebutton(GLFWwindow* window, int button, int action, int mods)
static void input_on_mousebutton(GLFWwindow* window, int button, int action, int mods)
{
}
void input_set_cursor_mode(Cursor_Mode mode)
void input_cursor_mode_set(Cursor_Mode mode)
{
GLFWwindow* window = window_get_active();
int cursor_mode = GLFW_CURSOR_NORMAL;
@ -56,17 +83,107 @@ void input_set_cursor_mode(Cursor_Mode mode)
glfwSetInputMode(window, GLFW_CURSOR, cursor_mode);
}
bool input_get_key_state(int key, int state_type)
bool input_map_state_get(const char* map_name, int state)
{
int current_state = KS_INACTIVE;
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
if(strcmp(map->name, map_name) == 0)
{
current_state = map->state;
break;
}
}
return state == current_state ? true : false;
}
bool inputkey_state_get(int key, int state_type)
{
GLFWwindow* window = window_get_active();
int current_state = glfwGetKey(window, key);
return current_state == state_type ? true : false;
}
bool input_get_mousebutton_state(int button, int state_type)
bool input_mousebutton_state_get(int button, int state_type)
{
GLFWwindow* window = window_get_active();
int current_state = glfwGetMouseButton(window, button);
return current_state == state_type ? true : false;
}
void input_map_create(const char* name, int* keys, size_t num_keys)
{
assert(name && keys && num_keys > 0);
Input_Map* new_map = array_add(input_map_list);
new_map->name = name;
new_map->keys = array_new(int);
new_map->state = KS_INACTIVE;
for(size_t i = 0; i < num_keys; i++)
{
int* key = array_add(new_map->keys);
*key = keys[i];
}
}
void input_update(void)
{
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
if(map->state == GLFW_RELEASE)
map->state = KS_INACTIVE;
}
}
bool input_map_remvove(const char* name)
{
assert(name);
bool success = false;
int index = map_find(name);
if(index > -1)
{
array_remove_at(input_map_list, (unsigned int)index);
success = true;
}
if(!success) log_error("input:map_remove", "Map %s not found", name);
return success;
}
bool input_map_keys_set(const char* name, int* keys, int num_keys)
{
assert(name && keys && num_keys > 0);
bool success = false;
int index = map_find(name);
if(index > -1)
{
Input_Map* map = array_get(input_map_list, (unsigned int)index);
array_reset(map->keys, num_keys);
for(int i = 0; i < num_keys; i++)
{
int* key = array_get(map->keys, (unsigned int)i);
*key = keys[i];
}
success = true;
}
if(!success) log_error("input:map_set_keys", "Map %s not found", name);
return success;
}
static int map_find(const char* name)
{
int index = -1;
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
if(strcmp(name, map->name) == 0)
{
index = i;
break;
}
}
return index;
}

@ -1,9 +1,8 @@
#ifndef input_H
#define input_H
#include <stdbool.h>
#include "array.h"
struct GLFWwindow;
typedef struct GLFWwindow GLFWwindow;
typedef enum
@ -11,13 +10,28 @@ typedef enum
CM_NORMAL = 0,
CM_LOCKED,
CM_HIDDEN
} Cursor_Mode;
typedef struct
{
Array* keys;
const char* name;
int state;
} Input_Map;
void input_init(GLFWwindow* window);
void input_cleanup(void);
bool input_get_mousebutton_state(int button, int state_type);
bool input_get_key_state(int key, int state_type);
void input_get_cursor_pos(double* xpos, double* ypos);
void input_set_cursor_mode(Cursor_Mode mode);
bool input_mousebutton_state_get(int button, int state_type);
bool input_key_state_get(int key, int state_type);
void input_cursor_pos_get(double* xpos, double* ypos);
void input_cursor_mode_set(Cursor_Mode mode);
void input_update(void);
bool input_map_state_get(const char* map_name, int state);
void input_map_create(const char* name, int* keys, size_t num_keys);
bool input_map_remvove(const char* name);
bool input_map_keys_set(const char* name, int* keys, int num_keys);
#endif

@ -23,3 +23,8 @@ void on_framebuffer_size_change(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void renderer_set_clearcolor(float red, float green, float blue, float alpha)
{
glClearColor(red, green, blue, alpha);
}

@ -6,5 +6,6 @@ typedef struct GLFWwindow GLFWwindow;
void renderer_init(GLFWwindow* window);
void renderer_draw(void);
void renderer_cleanup(void);
void renderer_set_clearcolor(float r, float g, float b, float a);
#endif

@ -22,8 +22,10 @@ bool window_init(const char* title, int width, int height)
}
else
{
const char* version = glfwGetVersionString();
log_message("Initialized with %s", version);
log_message("Initialized with GLFW version %d.%d.%d",
GLFW_VERSION_MAJOR,
GLFW_VERSION_MINOR,
GLFW_VERSION_REVISION);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
active_window = glfwCreateWindow(width, height, title, NULL, NULL);

Loading…
Cancel
Save