ソースを参照

Removed around 1000 SLOC of C for fun...

master
コミット
5f07cd341f
6個のファイルの変更0行の追加1631行の削除
  1. +0
    -8
      source/ai.f90
  2. +0
    -36
      source/makefile
  3. +0
    -272
      source/raylib.c
  4. +0
    -181
      source/sdl2.c
  5. +0
    -836
      source/system.c
  6. +0
    -298
      source/xcb.c

+ 0
- 8
source/ai.f90 ファイルの表示

@@ -1,8 +0,0 @@
subroutine ai_synchronize (level)

implicit none
integer, intent (in) :: level
write (*, '(a)', advance='no') "Heyo Fortran! "
print *, level

end subroutine ai_synchronize

+ 0
- 36
source/makefile ファイルの表示

@@ -1,36 +0,0 @@
.POSIX:

default:
gnatmake -g -O3 -fstack-check -c main.adb
gnatbind main.ali
gnatlink -o xhads main.ali -lraylib
mv xhads ../xhads
#~ gfortran -fPIC -Wall -Wextra -Ofast -fno-underscoring -fstack-check -c -o ai.o ai.f90
#~ gcc -g -ansi -Wall -Wextra -Wpedantic -Ofast -fstack-check -c -fPIC -o system.o system.c
#
#~ gnatmake -g -O3 -fstack-check -c main.adb
#~ gnatbind main.ali
#~ gcc -g -ansi -Wall -Wextra -Wpedantic -Ofast -fstack-check -c -fPIC -o raylib.o raylib.c
#~ gnatlink main.ali system.o raylib.o ai.o -o xhads -lraylib -lc -lgfortran
#~ gnatmake -g -O3 -fstack-check -c main.adb -lraylib
#~ mv xhads ../xhads
#~ gfortran -fPIC -Wall -Wextra -Ofast -fno-underscoring -fstack-check -c -o ai.o ai.f90
#~ gcc -g -ansi -Wall -Wextra -Wpedantic -Ofast -fstack-check -c -fPIC -o system.o system.c
#
#~ gnatmake -g -O3 -fstack-check -c main.adb
#~ gnatbind main.ali
#~ gcc -g -ansi -Wall -Wextra -Wpedantic -Ofast -fstack-check -c -fPIC -o raylib.o raylib.c
#~ gnatlink main.ali system.o raylib.o ai.o -o xhads -lraylib -lc -lgfortran
#~ mv xhads ../xhads
#
#~ gnatmake -g -O3 -fstack-check -c main.adb
#~ gnatbind main.ali
#~ gcc -g -ansi -Wall -Wextra -Wpedantic -Ofast -fstack-check -c -fPIC -o sdl2.o sdl2.c
#~ gnatlink main.ali system.o sdl2.o ai.o -o mam_sdl2 -lc -lgfortran -lSDL2 -lSDL2_image
#~ mv mam_sdl2 ../mam_sdl2
#
#~ gnatmake -g -O3 -fstack-check -c main.adb
#~ gnatbind main.ali
#~ gcc -g -ansi -Wall -Wextra -Wpedantic -Ofast -fstack-check -c -fPIC -o xcb.o xcb.c
#~ gnatlink main.ali system.o xcb.o ai.o -o mam_xcb -lc -lgfortran -lpng -lxcb -lxcb-image
#~ mv mam_xcb ../mam_xcb

+ 0
- 272
source/raylib.c ファイルの表示

@@ -1,272 +0,0 @@
#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) ? 4 : (32 - size) / 2) + x;
position.y = ((font == 0) ? 4 : (32 - size) / 2) + 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;*/
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]);
}

+ 0
- 181
source/sdl2.c ファイルの表示

@@ -1,181 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

static SDL_Texture * * render_texture;
static int render_texture_count = 0;

static SDL_Window * window;
static SDL_Renderer * renderer;
static SDL_Texture * font;
static SDL_Texture * mono;

static void render_clean_up (void) {
int i;

for (i = 0; i < render_texture_count; ++i) {
SDL_DestroyTexture (render_texture [i]);
}

free (render_texture);

SDL_DestroyTexture (font);
SDL_DestroyTexture (mono);

SDL_DestroyRenderer (renderer);
SDL_DestroyWindow (window);

SDL_Quit ();
}

extern int signal_x;
extern int signal_y;
extern int cursor_mode;
extern int signal_mode;

extern int engine_active;

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 signal_x = 0;
int signal_y = 0;
int cursor_mode = 0;
int signal_mode = 0;

int engine_active = 0;

int window_width (void) { int width = 0; SDL_GetWindowSize (window, & width, NULL); return (width); }
int window_height (void) { int height = 0; SDL_GetWindowSize (window, NULL, & height); return (height); }

void render_sprite (int sprite, int x, int y, int u, int v, int width, int height) {
SDL_Rect source, destination;

source.x = u;
source.y = v;
source.w = width;
source.h = height;

destination.x = x;
destination.y = y;
destination.w = (width < 0) ? -width : width;
destination.h = (height < 0) ? -height : height;

SDL_RenderCopy (renderer, render_texture [sprite], & source, & destination);
}

void render_string (char * string, int x, int y, int colour, char monospace) {
int i;

(void) colour;

for (i = 0; string [i] != '\0'; ++i) {
SDL_Rect source, destination;

source.x = (string [i] - 32) / 8 * 9;
source.y = (string [i] - 32) % 8 * 18;
source.w = 9;
source.h = 18;

destination.x = x + 7 + 9 * i;
destination.y = y + 7;
destination.w = 9;
destination.h = 18;

SDL_RenderCopy (renderer, (monospace != 0) ? mono : font, & source, & destination);
}
}

void engine_configure (void) {
engine_active = 1;

SDL_Init (SDL_INIT_VIDEO);

window = SDL_CreateWindow ("Chads of Might & Magic", 0, 0, 1800, 900, 0);
renderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_ACCELERATED);

font = IMG_LoadTexture (renderer, "./sprite/font/font.png");
mono = IMG_LoadTexture (renderer, "./sprite/font/mono.png");

atexit (render_clean_up);
}

void engine_synchronize (void) {
SDL_Event event;

SDL_RenderPresent (renderer);

SDL_GetMouseState (& signal_x, & signal_y);

SDL_PollEvent (& event);

switch (event.type) {
case SDL_QUIT:
engine_active = 0;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) cursor_mode = 1;
if (event.button.button == SDL_BUTTON_RIGHT) cursor_mode = 2;
if (event.button.button == SDL_BUTTON_MIDDLE) cursor_mode = 3;
break;
case SDL_MOUSEBUTTONUP:
cursor_mode = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP: signal_mode = 1; break;
case SDLK_DOWN: signal_mode = 2; break;
case SDLK_LEFT: signal_mode = 3; break;
case SDLK_RIGHT: signal_mode = 4; break;
case SDLK_ESCAPE: engine_active = 0; break;
default: signal_mode = 0; break;
}
break;
case SDL_KEYUP:
cursor_mode = 0;
break;
default:
break;
}

signal_mode = 0;

SDL_RenderClear (renderer);
}

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] = IMG_LoadTexture (renderer, path);

if (render_texture [render_texture_count - 1] == NULL) {
printf ("\033[1;31m%3i : '%60s';\033[0m\n", render_texture_count - 1, path);
}

return (render_texture_count - 1);
}

int sprite_width (int index) {
int width = 0;
SDL_QueryTexture (render_texture [index], NULL, NULL, & width, NULL);
return (width);
}

int sprite_height (int index) {
int height = 0;
SDL_QueryTexture (render_texture [index], NULL, NULL, NULL, & height);
return (height);
}

+ 0
- 836
source/system.c ファイルの表示

@@ -1,836 +0,0 @@
#ifndef null
#define null ((void *) 0)
#endif

enum {
false,
true
};

enum {
file_type_text, file_type_common_assembly, file_type_flat_assembly, file_type_gnu_assembly,
file_type_netwide_assembly, file_type_yet_another_assembly, file_type_c_source, file_type_c_header,
file_type_ada_body, file_type_ada_specification, file_type_cpp_source, file_type_cpp_header,
file_type_count
};

enum {
effect_normal, effect_bold, effect_italic, effect_underline, effect_blink, effect_reverse,
effect_count
};

enum {
colour_grey, colour_red, colour_green, colour_yellow, colour_blue, colour_pink, colour_cyan, colour_white,
colour_count
};

enum {
character_null, character_start_header, character_start_text, character_end_text,
character_end_transmission, character_enquiry, character_acknowledge, character_bell,
character_backspace, character_tab_horizontal, character_line_feed, character_tab_vertical,
character_form_feed, character_carriage_return, character_shift_out, character_shift_in,
character_data_link_escape, character_device_control_1, character_device_control_2, character_device_control_3,
character_device_control_4, character_not_acknowledge, character_synchronous_idle, character_end_transmission_block,
character_cancel, character_end_medium, character_substitute, character_escape,
character_file_separator, character_group_separator, character_record_separator, character_unit_separator
};

enum {
signal_none,
signal_any,
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_0, signal_1, signal_2, signal_3, signal_4, signal_5,
signal_6, signal_7, signal_8, signal_9, signal_escape, signal_tabulator, signal_return, signal_new_line,
signal_slash, signal_backslash, signal_quote, signal_backquote, signal_space, signal_backspace, signal_dot, signal_comma,
signal_cite, signal_caps_lock, signal_l_bracket, signal_r_bracket, signal_minus, signal_equal,
signal_count
};

static void out (void * data, int size);

extern void die (void);

extern void echo (char * data);

extern void fatal_failure (int condition, char * message);

extern void limit (int * value, int minimum, int maximum);

extern void * allocate (int size);
extern void * reallocate (void * data, int size);
extern void * deallocate (void * data);

extern int file_open (char * name, int mode);
extern int file_close (int file);
extern void file_read (int file, void * data, int size);
extern void file_write (int file, void * data, int size);
extern int file_seek (int file, int whence);
extern int file_size (char * name);
extern int file_type (char * name);
extern void * file_record (char * name);
extern char * file_import (char * name);
extern void file_export (char * name, void * data);

extern void file_list_import (char * name);
extern void file_list_export (char * name);
extern void file_list_insert_character (char character, int position);
extern void file_list_remove_character (int position);
extern void file_list_delete (void);

extern int character_compare_array (char character, char * character_array);

extern int character_count (char * string, char this, int from, int to, char stop);

extern int string_length (char * string);

extern char * string_reverse_limit (char * string, int limit);
extern char * string_reverse (char * string);

extern char * string_delete (char * string, int length);

extern int string_compare (char * string_0, char * string_1);
extern char * string_copy (char * string_0, char * string_1);
extern char * string_concatenate (char * string_0, char * string_1);

extern int string_compare_limit (char * string_0, char * string_1, int limit);
extern char * string_copy_limit (char * string_0, char * string_1, int limit);
extern char * string_concatenate_limit (char * string_0, char * string_1, int limit);

extern char * string_realign (char * string, int amount, char character);

extern void terminal_clear (void);
extern void terminal_colour (int colour, int effect);
extern void terminal_cancel (void);
extern void terminal_show_cursor (int show);

extern char * number_to_string (int number);
extern char * format_to_string (int number, int sigmoid, int base, int amount, char character);

extern int random_integer (int minimum, int maximum);

extern int syntax_define (int enrange, int derange, char * begin, char * end, char escape, int colour, int effect);

extern int syntax_select (char * string, int * length);

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>

static int file_list_active = 0;
static int file_list_count = 0;
static int * file_list_mark = null;
static int * file_list_size = null;
static char * * file_list_name = null;
static char * * file_list_data = null;

static int syntax_count = 0;
static int syntax_active = false;
static int * syntax_enrange = null;
static int * syntax_derange = null;
static char * * syntax_begin = null;
static char * * syntax_end = null;
static char * syntax_escape = null;
static int * syntax_colour = null;
static int * syntax_effect = null;

void out (void * data, int size) {
fatal_failure (data == null, "out: Failed to write to standard output, data is null pointer.");
fatal_failure (size == 0, "out: Failed to write to standard output, size is zero.");

(void) write (STDOUT_FILENO, data, (unsigned long int) size);
}

void die (void) {
exit (-1);
}

void echo (char * data) {
if (data == null) {
return;
}

out (data, string_length (data));
}

void fatal_failure (int condition, char * message) {
if (condition != 0) {
echo ("[\033[1;31mExiting\033[0m] ");
echo (message);
echo ("\n");

exit (EXIT_FAILURE);
}
}

void limit (int * value, int minimum, int maximum) {
if ( value == null ) { return; }
if (* value <= minimum) { * value = minimum; }
if (* value >= maximum) { * value = maximum; }
}

void * allocate (int size) {
char * data = null;

if (size <= 0) {
return (null);
}

data = calloc ((unsigned long int) size, sizeof (* data));

fatal_failure (data == null, "standard : allocate : Failed to allocate memory, internal function 'calloc' returned null pointer.");

return ((void *) data);
}

void * reallocate (void * data, int size) {
if (size <= 0) {
return (data);
}

data = realloc (data, (unsigned long int) size);

fatal_failure (data == null, "standard : reallocate: Failed to reallocate memory, internal function 'realloc' returned null pointer.");

/* Set new data to 0. */

return (data);
}

void * deallocate (void * data) {
if (data != null) {
free (data);
}

return (null);
}

int file_open (char * name, int mode) {
int descriptor = -1;

fatal_failure (name == null, "file_open: Failed to open file, name is null pointer.");

descriptor = open (name, mode);

fatal_failure (descriptor == -1, "file_open: Failed to open file, function open returned invalid descriptor.");

return (descriptor);
}

int file_close (int file) {
fatal_failure (file == -1, "file_close: Failed to close file, invalid file descriptor.");
fatal_failure (close (file) == -1, "file_close: Failed to close file, function close returned invalid code.");

return (-1);
}

void file_read (int file, void * data, int size) {
fatal_failure (file == -1, "file_read: Failed to read from file, invalid descriptor.");
fatal_failure (data == null, "file_read: Failed to read from file, data is null pointer.");
fatal_failure (size == 0, "file_read: Failed to read from file, size is zero.");

(void) read (file, data, (unsigned long int) size);
}

void file_write (int file, void * data, int size) {
fatal_failure (file == -1, "file_write: Failed to write to file, invalid descriptor.");
fatal_failure (data == null, "file_write: Failed to write to file, data is null pointer.");
fatal_failure (size == 0, "file_write: Failed to write to file, size is zero.");

(void) write (file, data, (unsigned long int) size);
}

int file_seek (int file, int whence) {
fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid descriptor.");

return ((int) lseek (file, 0, whence));
}

int file_size (char * name) {
int size = -1;
int file = -1;

file = file_open (name, O_RDONLY);

size = lseek (file, 0, SEEK_END);

fatal_failure (size == -1, "file_size: Failed to get size of file, invalid file size.");

file = file_close (file);

return (size);
}

int file_type (char * name) {
char * file_type_data [file_type_count] = {
".txt", ".s", ".fasm", ".gasm", ".nasm", ".yasm", ".c", ".h",
".adb", ".ads", ".cpp", ".hpp"
};

int type = 0;

while (* name != '.') {
++name;
}

for (type = 0; type != file_type_count; ++type) {
if (string_compare (name, file_type_data [type]) != 0) {
return (type);
}
}

return (-1);
}

void * file_record (char * name) {
int file = -1;
int size = -1;
char * data = null;

fatal_failure (name == null, "file_import: Failed to import file, name is null pointer.");

file = file_open (name, O_RDONLY);
size = file_size (name);
data = allocate (size);

file_read (file, data, size);

file = file_close (file);

return (data);
}

char * file_import (char * name) {
int file = -1;
int size = -1;
char * data = null;

fatal_failure (name == null, "file_import: Failed to import file, name is null pointer.");

file = file_open (name, O_RDONLY);
size = file_size (name) + 1;
data = allocate (size);

file_read (file, data, size - 1);

data [size - 1] = '\0';

file = file_close (file);

return (data);
}

void file_export (char * name, void * data) {
(void) name;
(void) data;
}

void file_list_import (char * name) {
fatal_failure (name == null, "file_list_import: Failed to import file, name is null pointer.");

++file_list_count;

file_list_active = file_list_count - 1;

file_list_mark = reallocate (file_list_mark, (int) sizeof (* file_list_mark) * file_list_count);
file_list_size = reallocate (file_list_size, (int) sizeof (* file_list_size) * file_list_count);
file_list_name = reallocate (file_list_name, (int) sizeof (* file_list_name) * file_list_count);
file_list_data = reallocate (file_list_data, (int) sizeof (* file_list_data) * file_list_count);

file_list_mark [file_list_active] = -1;
file_list_size [file_list_active] = -1;
file_list_name [file_list_active] = null;
file_list_data [file_list_active] = null;

file_list_name [file_list_active] = allocate (string_length (name) + 1);

(void) string_copy_limit (file_list_name [file_list_active], name, string_length (name) + 1);

file_list_mark [file_list_active] = open (name, O_RDWR);

fatal_failure (file_list_mark [file_list_active] == -1, "file_list_import: Failed to open file, function open returned invalid descriptor.");

file_list_size [file_list_active] = (int) lseek (file_list_mark [file_list_active], 0, SEEK_END) + 1;

(void) lseek (file_list_mark [file_list_active], 0, SEEK_SET);

file_list_data [file_list_active] = allocate (file_list_size [file_list_active]);

(void) read (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) (file_list_size [file_list_active] - 1));

close (file_list_mark [file_list_active]);

file_list_data [file_list_active] [file_list_size [file_list_active] - 1] = '\0';
}

void file_list_export (char * name) {
fatal_failure (name == null, "file_list_export: Failed to export file, name is null pointer.");

file_list_mark [file_list_active] = open (name, O_WRONLY | O_CREAT | O_TRUNC);

(void) write (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) file_list_size [file_list_active]);

close (file_list_mark [file_list_active]);
}

void file_list_insert_character (char character, int position) {
int offset;

++file_list_size [file_list_active];

if (file_list_size [file_list_active] < string_length (file_list_data [file_list_active])) {
file_list_data [file_list_active] = reallocate (file_list_data [file_list_active], file_list_size [file_list_active]);
}

file_list_data [file_list_active] = reallocate (file_list_data [file_list_active], file_list_size [file_list_active]);

for (offset = file_list_size [file_list_active] - 1; offset != position; --offset) {
file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset - 1];
}

file_list_data [file_list_active] [position] = character;
}

void file_list_remove_character (int position) {
int offset;

if (position == 0) {
return;
}

--file_list_size [file_list_active];

for (offset = position - 1; offset != file_list_size [file_list_active] - 1; ++offset) {
file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset + 1];
}

file_list_data [file_list_active] [offset] = '\0';
}

void file_list_delete (void) {
int i;

for (i = 0; i != file_list_count; ++i) {
file_list_name [i] = deallocate (file_list_name [i]);
file_list_data [i] = deallocate (file_list_data [i]);
}

file_list_mark = deallocate (file_list_mark);
file_list_size = deallocate (file_list_size);
file_list_name = deallocate (file_list_name);
file_list_data = deallocate (file_list_data);
}

int character_compare_array (char character, char * character_array) {
int i = 0;

do {
if (character == character_array [i]) {
return (1);
}
} while (++i != string_length (character_array));

return (0);
}

int character_count (char * string, char this, int from, int to, char stop) {
int count;

for (count = 0; (from != to) && (string [from] != stop); from += ((to < from) ? -1 : 1)) {
count += (int) ((string [from] == this) || (this == '\0'));
}

return (count);
}

int string_length (char * string) {
int length;

if (string == null) {
return (0);
}

for (length = 0; string [length] != '\0'; ++length);

return (length);
}

char * string_reverse_limit (char * string, int limit) {
int i;

fatal_failure (string == null, "string_reverse: String is null pointer.");

for (i = 0; i < limit / 2; ++i) {
char temporary = string [i];
string [i] = string [limit - 1 - i];
string [limit - 1 - i] = temporary;
}

return (string);
}

char * string_reverse (char * string) {
return (string_reverse_limit (string, string_length (string)));
}

char * string_delete (char * string, int length) {
int i;

if ((string == null) || (length <= 0)) {
return (string);
}

for (i = 0; i != length; ++i) {
string [i] = '\0';
}

return (string);
}

int string_compare (char * string_0, char * string_1) {
int i = 0;

fatal_failure (string_0 == null, "string_compare: Destination string is null pointer.");
fatal_failure (string_1 == null, "string_compare: Source string is null pointer.");

for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0'); ++i) {
if (string_0 [i] != string_1 [i]) {
return (0);
}
}

return (1);
}

char * string_copy (char * string_0, char * string_1) {
int i = 0;

fatal_failure (string_0 == null, "string_copy: Destination string is null pointer.");
fatal_failure (string_1 == null, "string_copy: Source string is null pointer.");

for (i = 0; i != string_length (string_1) + 1; ++i) {
string_0 [i] = string_1 [i];
}

return (string_0);
}

char * string_concatenate (char * string_0, char * string_1) {
fatal_failure (string_0 == null, "string_concatenate: Destination string is null pointer.");
fatal_failure (string_1 == null, "string_concatenate: Source string is null pointer.");

string_0 += string_length (string_0);

while (* string_1 != '\0') {
* string_0++ = * string_1++;
/*++string_0;
++string_1;*/
}

* string_0 = '\0';

return (string_0);
}

int string_compare_limit (char * string_0, char * string_1, int limit) {
int i = 0;

fatal_failure (string_0 == null, "string_compare_limit: Destination string is null pointer.");
fatal_failure (string_1 == null, "string_compare_limit: Source string is null pointer.");

for (i = 0; i != limit; ++i) {
if (string_0 [i] != string_1 [i]) {
return (0);
}
}

return (1);
}

char * string_copy_limit (char * string_0, char * string_1, int limit) {
int i = 0;

fatal_failure (string_0 == null, "string_copy_limit: Destination string is null pointer.");
fatal_failure (string_1 == null, "string_copy_limit: Source string is null pointer.");

if (limit <= 0) {
return (string_0);
}

for (i = 0; i != limit; ++i) {
string_0 [i] = string_1 [i];
}

return (string_0);
}

char * string_concatenate_limit (char * string_0, char * string_1, int limit) {
int i = 0;
int length_0 = 0;
int length_1 = 0;

fatal_failure (string_0 == null, "string_concatenate_limit: Destination string is null pointer.");
fatal_failure (string_1 == null, "string_concatenate_limit: Source string is null pointer.");

if (limit <= 0) {
return (string_0);
}

length_0 = string_length (string_0);
length_1 = string_length (string_1);

for (i = 0; (i != length_1) && (i != limit); ++i) {
string_0 [length_0 + i] = string_1 [i];
}

return (string_0);
}

char * string_realign (char * string, int amount, char character) {
int offset, length;

length = string_length (string);

for (offset = 0; offset != length; ++offset) {
string [amount - offset - 1] = string [length - offset - 1];
}

for (offset = 0; offset != amount - length; ++offset) {
string [offset] = character;
}

string [amount] = '\0';

return (string);
}

void terminal_clear (void) {
echo ("\033[2J\033[H");
}

void terminal_colour (int colour, int effect) {
char format [8] = "\033[ ;3 m";

format [2] = (char) (effect % effect_count) + '0';
format [5] = (char) (colour % colour_count) + '0';

echo (format);
}

void terminal_cancel (void) {
echo ("\033[0m");
}

void terminal_show_cursor (int show) {
if (show != 0) {
echo ("\033[?25h");
} else {
echo ("\033[?25l");
}
}

char * number_to_string (int number) {
int i, sigmoid;

static char string [32];

string_delete (string, 32);

if (number == 0) {
string [0] = '0';
string [1] = '\0';
return (string);
}

if (number < 0) {
number *= -1;
sigmoid = 1;
} else {
sigmoid = 0;
}

for (i = (string [0] == '-'); number != 0; ++i) {
string [i] = (char) (number % 10) + '0';
number /= 10;
}

if (sigmoid != 0) {
string [i] = '-';
++i;
}

string [i] = '\0';

string_reverse (string);

return (string);
}

char * format_to_string (int number, int sigmoid, int base, int amount, char character) {
int i;

static char string [32];

string_delete (string, 32);

if (number == 0) {
string [0] = '0';
string [1] = '\0';

string_realign (string, amount, character);

return (string);
}

if (number < 0) {
number *= -1;
}

for (i = (string [0] == '-'); number != 0; ++i) {
string [i] = "0123456789ABCDEF" [number % base];
number /= base;
}

if (sigmoid != 0) {
string [i] = '-';
++i;
}

string [i] = '\0';

string_reverse (string);

string_realign (string, amount, character);

return (string);
}

static void syntax_delete (void) {
int offset;

if (syntax_active == false) {
return;
}

for (offset = 0; offset < syntax_count; ++offset) {
syntax_begin [offset] = deallocate (syntax_begin [offset]);
syntax_end [offset] = deallocate (syntax_end [offset]);
}

syntax_enrange = deallocate (syntax_enrange);
syntax_derange = deallocate (syntax_derange);
syntax_begin = deallocate (syntax_begin);
syntax_end = deallocate (syntax_end);
syntax_escape = deallocate (syntax_escape);
syntax_colour = deallocate (syntax_colour);
syntax_effect = deallocate (syntax_effect);

syntax_active = false;
syntax_count = 0;
}

int random_integer (int minimum, int maximum) {
return (((int) rand () % (maximum - minimum + 1)) + minimum);
}

int syntax_define (int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
if (syntax_active == false) {
syntax_active = true;

atexit (syntax_delete);
}

fatal_failure (begin == null, "syntax_define: Begin string is null pointer.");
fatal_failure (end == null, "syntax_define: End string is null pointer.");

++syntax_count;

syntax_enrange = reallocate (syntax_enrange, syntax_count * (int) sizeof (* syntax_enrange));
syntax_derange = reallocate (syntax_derange, syntax_count * (int) sizeof (* syntax_derange));
syntax_begin = reallocate (syntax_begin, syntax_count * (int) sizeof (* syntax_begin));
syntax_end = reallocate (syntax_end, syntax_count * (int) sizeof (* syntax_end));
syntax_escape = reallocate (syntax_escape, syntax_count * (int) sizeof (* syntax_escape));
syntax_colour = reallocate (syntax_colour, syntax_count * (int) sizeof (* syntax_colour));
syntax_effect = reallocate (syntax_effect, syntax_count * (int) sizeof (* syntax_effect));

syntax_begin [syntax_count - 1] = allocate ((string_length (begin) + 1) * (int) sizeof (* * syntax_begin));
syntax_end [syntax_count - 1] = allocate ((string_length (end) + 1) * (int) sizeof (* * syntax_end));

syntax_enrange [syntax_count - 1] = enrange;
syntax_derange [syntax_count - 1] = derange;
syntax_escape [syntax_count - 1] = escape;
syntax_colour [syntax_count - 1] = colour;
syntax_effect [syntax_count - 1] = effect;

string_copy (syntax_begin [syntax_count - 1], begin);
string_copy (syntax_end [syntax_count - 1], end);

return (syntax_count - 1);
}

int syntax_select (char * string, int * length) {
int offset, subset, select;

fatal_failure (syntax_active == false, "syntax_select: Syntax is not active.");
fatal_failure (string == null, "syntax_select: String is null.");
fatal_failure (length == null, "syntax_select: Length is null.");

for (select = offset = 0; select != syntax_count; ++select) {
if (syntax_enrange [select] == false) {
if (syntax_derange [select] == false) {
if (string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select])) == true) {
break;
}
} else {
if ((string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select])) == true)
&& (character_compare_array (string [offset + string_length (syntax_begin [select])], syntax_end [select]) == true)) {
break;
}
}
} else {
for (subset = 0; subset != string_length (syntax_begin [select]); ++subset) {
if (string [offset] == syntax_begin [select] [subset]) {
goto selected;
}
}
}
}

selected:

if (select >= syntax_count) {
* length = 1;

return (syntax_count);
}

for (offset = 1; string [offset - 1] != '\0'; ++offset) {
if (string [offset] == syntax_escape [select]) {
++offset;
continue;
}

if (syntax_derange [select] == false) {
if (string_compare_limit (& string [offset], syntax_end [select], string_length (syntax_end [select])) == true) {
* length = offset + string_length (syntax_end [select]);
return (select);
}
} else {
subset = 0;
if (string_compare (syntax_end [select], "") == false) {
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
* length = offset;
goto finished;
}
} while (++subset != string_length (syntax_end [select]));
}
}

finished:

return (select);
}

+ 0
- 298
source/xcb.c ファイルの表示

@@ -1,298 +0,0 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <png.h>

#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_image.h>

#define FONT_WIDTH ( 9)
#define FONT_HEIGHT (18)

static unsigned int * * texture_array = NULL;
static int * texture_array_w = NULL;
static int * texture_array_h = NULL;
static int texture_count = 0;

static unsigned int * font = NULL;
static unsigned int * mono = NULL;

static int screen_width = 1800;
static int screen_height = 900;

static xcb_window_t window = 0;
static xcb_gcontext_t context = 0;
static xcb_pixmap_t pixmap = 0;
static xcb_connection_t * connection = NULL;
static xcb_screen_t * screen = NULL;
static xcb_image_t * image = NULL;
static unsigned int * framebuffer = NULL;

static unsigned int * import_png (char * path, int * w, int * h) {
unsigned int buffer [1024*1024] = { 0 };
unsigned int * data = NULL;
png_image temporary = { 0 };
temporary.version = PNG_IMAGE_VERSION;
if (! png_image_begin_read_from_file (& temporary, path)) printf ("PNG: image begin read from file fuck up!\n");
temporary.format = PNG_FORMAT_BGRA;
if (! png_image_finish_read (& temporary, NULL, buffer, 0, NULL)) printf ("PNG: image finish read fuck up!\n");
if (w != NULL) * w = temporary.width;
if (h != NULL) * h = temporary.height;
if (buffer == NULL) printf ("PNG: empty buffer fuck up!\n");
png_image_free (& temporary);
data = calloc (temporary.width * temporary.height, sizeof (* data));
if (data == NULL) printf ("PNG: empty data fuck up!\n");
memcpy (data, buffer, temporary.width * temporary.height * sizeof (* data));
return (data);
}

static void render_clean_up (void) {
int i;

printf ("Deinitializing graphical components...\n");

for (i = 0; i < texture_count; ++i) {
free (texture_array [i]);
texture_array [i] = NULL;
}

free (texture_array);

free (framebuffer);

free (font);
free (mono);

texture_array = NULL;
font = NULL;
mono = NULL;

xcb_free_gc (connection, context);

xcb_free_pixmap (connection, pixmap);

framebuffer = NULL;

xcb_destroy_window (connection, window);

xcb_disconnect (connection);
}

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 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 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 (screen_width); }
int window_height (void) { return (screen_height); }

void render_sprite (int sprite, int x, int y, int u, int v, int width, int height) {
int sprite_x, sprite_y;

width = (width < 0) ? -width : width;
height = (height < 0) ? -height : height;

if ((x < 0) || (y < 0)
|| (x > window_width () - width) || (y > window_height () - height)) {
return;
}

for (sprite_y = 0; sprite_y < height; ++sprite_y) {
for (sprite_x = 0; sprite_x < width; ++sprite_x) {
int o = (sprite_y + y) * window_width () + (sprite_x + x);
int s = (sprite_y + v) * sprite_width (sprite) + (sprite_x + u);
framebuffer [o] = ((texture_array [sprite] [s] & 0XFF000000) < 0X04) ?
framebuffer [o] : texture_array [sprite] [s];
}
}
}

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) string;
(void) x;
(void) y;
(void) colour;
(void) monospace;
}

void render_vector (int x1, int y1, int x2, int y2) {
(void) x1;
(void) y1;
(void) x2;
(void) y2;
}

void engine_configure (void) {
unsigned int values [2] = {
0,
XCB_EVENT_MASK_NO_EVENT |
XCB_EVENT_MASK_EXPOSURE |
XCB_EVENT_MASK_RESIZE_REDIRECT |
XCB_EVENT_MASK_KEY_RELEASE |
XCB_EVENT_MASK_KEY_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE |
XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_POINTER_MOTION
};

engine_active = 1;

printf ("Initializing graphical components...\n");

font = import_png ("./sprite/font/font.png", NULL, NULL);
mono = import_png ("./sprite/font/mono.png", NULL, NULL);

connection = xcb_connect (NULL, NULL);

screen = xcb_setup_roots_iterator (xcb_get_setup (connection)) . data;

values [0] = screen -> black_pixel;

window = xcb_generate_id (connection);
context = xcb_generate_id (connection);
pixmap = xcb_generate_id (connection);

xcb_create_window (connection, screen -> root_depth, window, screen -> root, 0, 0, (uint16_t) window_width (), (uint16_t) window_height (), 10,
XCB_WINDOW_CLASS_INPUT_OUTPUT, screen -> root_visual, XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, values);

xcb_map_window (connection, window);

xcb_change_property (connection, XCB_PROP_MODE_REPLACE, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8,
sizeof ("-- Chads of Might & Magic --"), "-- Chads of Might & Magic --");

xcb_create_pixmap (connection, screen -> root_depth, pixmap, window, (uint16_t) window_width (), (uint16_t) window_height ());

xcb_create_gc (connection, context, pixmap, 0, NULL);

xcb_flush (connection);

atexit (render_clean_up);
}

void engine_synchronize (void) {
xcb_generic_event_t * event = NULL;

if (framebuffer != NULL) {
image = xcb_image_create_native (connection, (uint16_t) screen_width, (uint16_t) screen_height, XCB_IMAGE_FORMAT_Z_PIXMAP, screen -> root_depth,
framebuffer, (uint32_t) (screen_width * screen_height * (int32_t) sizeof (* framebuffer)), (uint8_t *) framebuffer);

xcb_image_put (connection, pixmap, context, image, 0, 0, 0);

xcb_image_destroy (image);

xcb_copy_area (connection, pixmap, window, context, 0, 0, 0, 0, (uint16_t) screen_width, (uint16_t) screen_height);
}

event = xcb_wait_for_event (connection);

if (event == NULL) {
return;
} else if ((event->response_type & 127) == XCB_EXPOSE) {
xcb_flush (connection);
} else if ((event->response_type & 127) == XCB_KEY_PRESS) {
printf ("Key: %i\n", ((xcb_key_press_event_t *) event)->detail);
switch (((xcb_key_press_event_t *) event)->detail) {
case 24: engine_active = 0; break;
case 25: cursor_mode = 4; break;
case 26: cursor_mode = 5; break;
case 27: cursor_mode = 6; break;
case 28: cursor_mode = 7; break;
default: break;
}
} else if ((event->response_type & 127) == XCB_BUTTON_PRESS) {
xcb_button_press_event_t * button_press_event = (xcb_button_press_event_t *) event;
cursor_mode = button_press_event->detail;
} else if ((event->response_type & 127) == XCB_MOTION_NOTIFY) {
xcb_motion_notify_event_t * button_press_event = (xcb_motion_notify_event_t *) event;
cursor.x = button_press_event->event_x;
cursor.y = button_press_event->event_y;
}

free (event);

framebuffer = calloc ((size_t) (screen_width * screen_height), sizeof (* framebuffer));

signal_mode = 0;
}

int import_texture (char * path) {
++texture_count;

texture_array = realloc (texture_array, (unsigned long int) texture_count * sizeof (* texture_array));
texture_array_w = realloc (texture_array_w, (unsigned long int) texture_count * sizeof (* texture_array_w));
texture_array_h = realloc (texture_array_h, (unsigned long int) texture_count * sizeof (* texture_array_h));

texture_array [texture_count - 1] = import_png (path, & texture_array_w [texture_count - 1], & texture_array_h [texture_count - 1]);

if (texture_array [texture_count - 1] == NULL) {
printf ("\033[1;31m%3i : '%60s';\033[0m\n", texture_count - 1, path);
}

return (texture_count - 1);
}

int import_sound (char * path) {
(void) path;
return (0);
}

int sprite_width (int index) {
return (texture_array_w [index]);
}

int sprite_height (int index) {
return (texture_array_h [index]);
}

void play_sound (int index) { (void) index; }
void stop_sound (int index) { (void) index; }
void loop_sound (int index) { (void) index; }

読み込み中…
キャンセル
保存