Created
September 24, 2019 15:43
-
-
Save amirrajan/5a3f3f052861503c7f0d56535a75d1a7 to your computer and use it in GitHub Desktop.
Data_Get_Struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ffi.h" | |
#include "mrb_runtime.h" | |
#include <mruby/data.h> | |
#include <mruby/string.h> | |
#include <mruby/array.h> | |
#include <mruby/irep.h> | |
#include <SDL_image.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <string.h> | |
typedef struct { | |
SDL_Context *sdl_context; | |
} FFI; | |
static FFI ffi; | |
static void free_texture(mrb_state *mrb, void *p_); | |
static const struct mrb_data_type texture_data_type = { "texture", free_texture }; | |
static mrb_value ffi_sdl_draw_line(mrb_state *mrb, mrb_value self) | |
{ | |
mrb_value tuple; | |
mrb_get_args(mrb, "o", &tuple); | |
SDL_SetRenderDrawColor(ffi.sdl_context->renderer, | |
255, | |
255, | |
255, | |
255); | |
long long x, y, x_2, y_2 = 0; | |
x = mrb_fixnum(mrb_ary_entry(tuple, 0)); | |
y = mrb_fixnum(mrb_ary_entry(tuple, 1)); | |
x_2 = mrb_fixnum(mrb_ary_entry(tuple, 2)); | |
y_2 = mrb_fixnum(mrb_ary_entry(tuple, 3)); | |
SDL_RenderDrawLine(ffi.sdl_context->renderer, x, y, x_2, y_2); | |
return mrb_nil_value(); | |
} | |
static mrb_value ffi_sdl_draw_sprite(mrb_state *mrb, mrb_value self) | |
{ | |
mrb_value tuple; | |
mrb_get_args(mrb, "o", &tuple); | |
long long x, y, w, h = 0; | |
double angle; | |
mrb_value wrapped_texture = mrb_ary_entry(tuple, 0); | |
x = mrb_fixnum(mrb_ary_entry(tuple, 1)); | |
y = mrb_fixnum(mrb_ary_entry(tuple, 2)); | |
w = mrb_fixnum(mrb_ary_entry(tuple, 3)); | |
h = mrb_fixnum(mrb_ary_entry(tuple, 4)); | |
angle = mrb_float(mrb_ary_entry(tuple, 5)); | |
SDL_Texture *texture; | |
Data_Get_Struct(mrb, wrapped_texture, &texture_data_type, texture); | |
SDL_Rect destination_frame; | |
destination_frame.x = x; | |
destination_frame.y = y; | |
destination_frame.w = w; | |
destination_frame.h = h; | |
SDL_RenderCopyEx(ffi.sdl_context->renderer, | |
texture, NULL, | |
&destination_frame, | |
angle, | |
NULL, | |
SDL_FLIP_NONE); | |
return mrb_nil_value(); | |
} | |
static mrb_value ffi_file_mtime(mrb_state *mrb, mrb_value self) | |
{ | |
mrb_value path; | |
mrb_get_args(mrb, "o", &path); | |
struct stat attr; | |
stat(RSTRING_PTR(path), &attr); | |
return mrb_fixnum_value(attr.st_mtime); | |
} | |
static mrb_value ffi_mrb_reload_file(mrb_state *mrb, mrb_value self) | |
{ | |
mrb_value path; | |
mrb_get_args(mrb, "o", &path); | |
FILE *fp = fopen(RSTRING_PTR(path), "r"); | |
mrb_load_file(mrb, fp); | |
fclose(fp); | |
return mrb_nil_value(); | |
} | |
static mrb_value ffi_sdl_create_texture(mrb_state *mrb, mrb_value self) | |
{ | |
mrb_value path; | |
mrb_get_args(mrb, "o", &path); | |
SDL_Texture *texture = IMG_LoadTexture(ffi.sdl_context->renderer, RSTRING_PTR(path)); | |
return mrb_obj_value(Data_Wrap_Struct(mrb, mrb->object_class, &texture_data_type, texture)); | |
} | |
static void ffi_init_sdl_class() | |
{ | |
ffi.sdl_class = | |
mrb_define_class_under(ffi.runtime->mrb, | |
ffi.module, | |
"SDL", | |
ffi.runtime->mrb->object_class); | |
mrb_define_method(ffi.runtime->mrb, | |
ffi.sdl_class, | |
"draw_sprite", | |
ffi_sdl_draw_sprite, | |
MRB_ARGS_REQ(1)); | |
mrb_define_method(ffi.runtime->mrb, | |
ffi.sdl_class, | |
"draw_line", | |
ffi_sdl_draw_line, | |
MRB_ARGS_REQ(1)); | |
mrb_define_method(ffi.runtime->mrb, | |
ffi.sdl_class, | |
"create_texture", | |
ffi_sdl_create_texture, | |
MRB_ARGS_REQ(1)); | |
} | |
static void ffi_init_file_class() | |
{ | |
ffi.file_class = | |
mrb_define_class_under(ffi.runtime->mrb, | |
ffi.module, | |
"File", | |
ffi.runtime->mrb->object_class); | |
mrb_define_method(ffi.runtime->mrb, | |
ffi.file_class, | |
"mtime", | |
ffi_file_mtime, | |
MRB_ARGS_REQ(1)); | |
} | |
static void ffi_init_mrb_class() | |
{ | |
ffi.mrb_class = | |
mrb_define_class_under(ffi.runtime->mrb, | |
ffi.module, | |
"MRB", | |
ffi.runtime->mrb->object_class); | |
mrb_define_method(ffi.runtime->mrb, | |
ffi.mrb_class, | |
"reload", | |
ffi_mrb_reload_file, | |
MRB_ARGS_REQ(1)); | |
} | |
void ffi_init(MRB_Runtime *runtime, SDL_Context *sdl_context) | |
{ | |
ffi.sdl_context = sdl_context; | |
ffi.runtime = runtime; | |
ffi.module = mrb_define_module(runtime->mrb, "FFI"); | |
ffi_init_sdl_class(); | |
ffi_init_file_class(); | |
ffi_init_mrb_class(); | |
} | |
static void free_texture(mrb_state *mrb, void *p_) | |
{ | |
SDL_Texture *texture = (SDL_Texture *)p_; | |
SDL_DestroyTexture(texture); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment