xhads/source/raylib.c

203 lines
6.7 KiB
C

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <raylib.h>
enum {
signal_none, signal_space, signal_zero, signal_one, signal_two, signal_three,
signal_four, signal_five, signal_six, signal_seven, signal_eight, signal_nine,
signal_a, signal_b, signal_c, signal_d, signal_e, signal_f,
signal_g, signal_h, signal_i, signal_j, signal_k, signal_l,
signal_m, signal_n, signal_o, signal_p, signal_q, signal_r,
signal_s, signal_t, signal_u, signal_v, signal_w, signal_x,
signal_y, signal_z, signal_grave, signal_escape, signal_enter, signal_tab,
signal_backspace, signal_right, signal_left, signal_down, signal_up, signal_kp_0,
signal_kp_1, signal_kp_2, signal_kp_3, signal_kp_4, signal_kp_5, signal_kp_6,
signal_kp_7, signal_kp_8, signal_kp_9, signal_kp_subtract, signal_kp_add, signal_left_shift,
signal_left_control, signal_count
};
static Texture2D * render_texture;
static int render_texture_count = 0;
static Font font = { 0 };
static Font mono = { 0 };
static Color tint = { 255, 255, 255, 255 };
static Vector2 dump = { 0, 0 };
static void no_logging (int msgType, const char * text, va_list args) {
(void) msgType;
(void) text;
(void) args;
return;
}
static void render_clean_up (void) {
free (render_texture);
CloseWindow ();
}
extern int cursor_x;
extern int cursor_y;
extern int cursor_mode;
extern int signal_mode;
extern int engine_active;
extern int framerate;
extern int window_width (void);
extern int window_height (void);
extern void render_sprite (int sprite, int x, int y, int u, int v, int width, int height);
extern void render_string (char * string, int x, int y, int colour, char monospace);
extern void engine_configure (void);
extern void engine_synchronize (void);
extern int import_sprite (char * path);
extern int sprite_width (int index);
extern int sprite_height (int index);
int cursor_x = 0;
int cursor_y = 0;
int cursor_mode = 0;
int signal_mode = 0;
int engine_active = 0;
int framerate = 0;
int window_width (void) { return (GetScreenWidth ()); }
int window_height (void) { return (GetScreenHeight ()); }
void render_sprite (int sprite, int x, int y, int u, int v, int width, int height) {
Rectangle source, destination;
source.x = u;
source.y = v;
source.width = width;
source.height = height;
destination.x = x;
destination.y = y;
destination.width = (width < 0) ? -width : width;
destination.height = (height < 0) ? -height : height;
DrawTexturePro (render_texture [sprite], source, destination, dump, 0.0, tint);
}
void render_string (char * string, int x, int y, int colour, char monospace) {
Vector2 position = { 4, 4 };
Color new_tint = { 255, 255, 255, 255 };
position.x += x;
position.y += y;
new_tint.r = ((colour & 0XFF0000) >> 16) % 256;
new_tint.g = ((colour & 0X00FF00) >> 8) % 256;
new_tint.b = ((colour & 0X0000FF) >> 0) % 256;
DrawTextPro ((monospace != 0) ? mono : font, string, position, dump, 0.0, 24, 6, new_tint);
}
void engine_configure (void) {
engine_active = 1;
SetTraceLogCallback (no_logging);
InitWindow (1800, 900, "Chads of Might & Magic");
SetTargetFPS (60);
font = LoadFont ("./sprite/font/gothic.ttf");
mono = LoadFont ("./sprite/font/mono.ttf");
atexit (render_clean_up);
}
void engine_synchronize (void) {
int signal = signal_none;
Color background = { 50, 60, 70, 100 };
framerate = (int) GetFPS ();
EndDrawing ();
if (WindowShouldClose ()) {
engine_active = 0;
}
cursor_x = GetMouseX ();
cursor_y = GetMouseY ();
if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT)) { cursor_mode = 1; }
if (IsMouseButtonPressed (MOUSE_BUTTON_RIGHT)) { cursor_mode = 2; }
if (IsMouseButtonPressed (MOUSE_BUTTON_MIDDLE)) { cursor_mode = 3; }
if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT)) { cursor_mode = 0; }
if (IsMouseButtonReleased (MOUSE_BUTTON_RIGHT)) { cursor_mode = 0; }
if (IsMouseButtonReleased (MOUSE_BUTTON_MIDDLE)) { cursor_mode = 0; }
signal = GetKeyPressed ();
switch (signal) {
case KEY_NULL: signal_mode = signal_none; break;
case KEY_SPACE: signal_mode = signal_space; break;
case KEY_GRAVE: signal_mode = signal_grave; break;
case KEY_LEFT_SHIFT: signal_mode = signal_left_shift; break;
case KEY_LEFT_CONTROL: signal_mode = signal_left_control; break;
case KEY_KP_SUBTRACT: signal_mode = signal_kp_subtract; break;
case KEY_KP_ADD: signal_mode = signal_kp_add; break;
case KEY_ESCAPE: signal_mode = signal_escape; break;
case KEY_ENTER: signal_mode = signal_enter; break;
case KEY_TAB: signal_mode = signal_tab; break;
case KEY_BACKSPACE: signal_mode = signal_backspace; break;
case KEY_RIGHT: signal_mode = signal_right; break;
case KEY_LEFT: signal_mode = signal_left; break;
case KEY_DOWN: signal_mode = signal_down; break;
case KEY_UP: signal_mode = signal_up; break;
default: {
if ((signal >= KEY_ZERO) && (signal <= KEY_NINE)) {
signal_mode = signal - KEY_ZERO + signal_zero;
} else if ((signal >= KEY_KP_0) && (signal <= KEY_KP_9)) {
signal_mode = signal - KEY_KP_0 + signal_kp_0;
} else if ((signal >= KEY_A) && (signal <= KEY_Z)) {
signal_mode = signal - KEY_A + signal_a;
} else {
signal_mode = signal_none;
}
}
}
BeginDrawing ();
ClearBackground (background);
}
int import_sprite (char * path) {
++render_texture_count;
render_texture = realloc (render_texture, (unsigned long int) render_texture_count * sizeof (* render_texture));
render_texture [render_texture_count - 1] = LoadTexture (path);
if ((render_texture [render_texture_count - 1].width == 0) || (render_texture [render_texture_count - 1].height == 0)) {
printf ("\033[1;31m%3i : '%60s' := %3i, %3i;\033[0m\n",
render_texture_count - 1,
path,
render_texture [render_texture_count - 1].width,
render_texture [render_texture_count - 1].height);
}
return (render_texture_count - 1);
}
int sprite_width (int index) {
return (render_texture [index].width);
}
int sprite_height (int index) {
return (render_texture [index].height);
}