xhads/source/raylib.c

272 lines
8.2 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 int texture_count = 0;
static int sound_count = 0;
static int font_count = 1;
static Texture2D * texture_array;
static Sound * sound_array;
static Font * font_array;
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) {
int i;
for (i = 0; i < texture_count; ++i) {
UnloadTexture (texture_array [i]);
}
for (i = 0; i < sound_count; ++i) {
UnloadSound (sound_array [i]);
}
for (i = 1; i < font_count; ++i) {
UnloadFont (font_array [i]);
}
free (texture_array);
free (sound_array);
free (font_array);
CloseAudioDevice ();
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, int font, int size, int pad);
extern void render_vector (int x1, int y1, int x2, int y2);
extern void engine_configure (void);
extern void engine_synchronize (void);
extern int import_texture (char * path);
extern int import_sound (char * path);
extern int import_font (char * path);
extern int sprite_width (int index);
extern int sprite_height (int index);
extern void play_sound (int index);
extern void stop_sound (int index);
extern void loop_sound (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 (texture_array [sprite], source, destination, dump, 0.0, tint);
}
void render_string (char * string, int x, int y, int colour, int font, int size, int pad) {
Color new_tint = { 255, 255, 255, 255 };
Vector2 position;
position.x = ((font == 0) ? 6 : pad) + x;
position.y = ((font == 0) ? 6 : pad) + y;
new_tint.r = ((colour & 0XFF0000) >> 16) % 256;
new_tint.g = ((colour & 0X00FF00) >> 8) % 256;
new_tint.b = ((colour & 0X0000FF) >> 0) % 256;
if (font == 0) {
DrawTextPro (GetFontDefault (), string, position, dump, 0.0, 22, 4, new_tint);
} else {
DrawTextPro (font_array [font], string, position, dump, 0.0, size, pad, new_tint);
}
}
void render_vector (int x1, int y1, int x2, int y2) {
DrawLine (x1, y1, x2, y2, BLACK);
}
void engine_configure (void) {
engine_active = 1;
SetTraceLogCallback (no_logging);
InitWindow (1800, 900, "Chads of Might & Magic");
SetTargetFPS (60);
InitAudioDevice ();
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_texture (char * path) {
++texture_count;
texture_array = realloc (texture_array, (unsigned long int) texture_count * sizeof (* texture_array));
texture_array [texture_count - 1] = LoadTexture (path);
if ((texture_array [texture_count - 1].width == 0) || (texture_array [texture_count - 1].height == 0)) {
printf ("\033[1;31m%3i : '%60s' := %3i, %3i;\033[0m\n",
texture_count - 1,
path,
texture_array [texture_count - 1].width,
texture_array [texture_count - 1].height);
}
return (texture_count - 1);
}
int import_sound (char * path) {
++sound_count;
sound_array = realloc (sound_array, (unsigned long int) sound_count * sizeof (* sound_array));
sound_array [sound_count - 1] = LoadSound (path);
return (sound_count - 1);
}
int import_font (char * path) {
++font_count;
font_array = realloc (font_array, (unsigned long int) font_count * sizeof (* font_array));
font_array [font_count - 1] = LoadFont (path);
/*
GenTextureMipmaps (& font_array [font_count - 1].texture);
SetTextureFilter (font_array [font_count - 1].texture, TEXTURE_FILTER_POINT);
*/
return (font_count - 1);
}
int sprite_width (int index) {
return (texture_array [index].width);
}
int sprite_height (int index) {
return (texture_array [index].height);
}
void play_sound (int index) {
PlaySound (sound_array [index]);
}
void stop_sound (int index) {
StopSound (sound_array [index]);
}
void loop_sound (int index) {
PlaySound (sound_array [index]);
}