Rug 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +32 -0
- data/Rakefile +19 -0
- data/ext/extconf.rb +14 -0
- data/ext/rug.c +117 -0
- data/ext/rug_conf.c +104 -0
- data/ext/rug_conf.h +18 -0
- data/ext/rug_events.c +124 -0
- data/ext/rug_events.h +10 -0
- data/ext/rug_layer.c +54 -0
- data/ext/rug_layer.h +15 -0
- data/ext/rug_resources.c +104 -0
- data/ext/rug_resources.h +9 -0
- data/lib/Rug.rb +1 -0
- metadata +79 -0
data/README
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Rug is a basic gaming library for Ruby, based on Love2D for Lua. The goal is to promote
|
|
2
|
+
rapid game development by handling the boiler plate details under the hood and leave the
|
|
3
|
+
fun stuff for the programmer.
|
|
4
|
+
|
|
5
|
+
============================
|
|
6
|
+
Dependencies
|
|
7
|
+
============================
|
|
8
|
+
|
|
9
|
+
In order to build Rug, you need a few dependencies:
|
|
10
|
+
- Ruby development headers
|
|
11
|
+
- SDL
|
|
12
|
+
- SDL_image
|
|
13
|
+
|
|
14
|
+
In Ubuntu, you can install these with apt-get:
|
|
15
|
+
|
|
16
|
+
sudo apt-get install ruby-dev libsdl1.2-dev libsdl-image1.2-dev
|
|
17
|
+
|
|
18
|
+
============================
|
|
19
|
+
Installation
|
|
20
|
+
============================
|
|
21
|
+
|
|
22
|
+
At the moment only Linux has been tested, but the library should work on any system
|
|
23
|
+
that supports both Ruby and SDL. You need the SDL libraries installed in order to
|
|
24
|
+
build Rug.
|
|
25
|
+
|
|
26
|
+
To build:
|
|
27
|
+
|
|
28
|
+
ruby extconf.rb
|
|
29
|
+
make
|
|
30
|
+
|
|
31
|
+
At the moment there are no pre-built versions of Rug for you to download. These will
|
|
32
|
+
be made available as soon as possible.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake/gempackagetask'
|
|
3
|
+
|
|
4
|
+
spec = Gem::Specification.new do |s|
|
|
5
|
+
s.name = "Rug"
|
|
6
|
+
s.version = "0.0.2"
|
|
7
|
+
s.author = "Rob Britton"
|
|
8
|
+
s.email = "rob.s.brit@gmail.com"
|
|
9
|
+
s.platform = Gem::Platform::RUBY
|
|
10
|
+
s.summary = "A simple game framework."
|
|
11
|
+
s.files = FileList["Rakefile", "lib/*.rb", "ext/*.{c,h}", "README"].to_a
|
|
12
|
+
s.require_path = "lib"
|
|
13
|
+
s.has_rdoc = false
|
|
14
|
+
s.extensions = ["ext/extconf.rb"]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
|
18
|
+
pkg.need_tar = true
|
|
19
|
+
end
|
data/ext/extconf.rb
ADDED
data/ext/rug.c
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
#include "ruby.h"
|
|
2
|
+
|
|
3
|
+
#include "rug_conf.h"
|
|
4
|
+
#include "rug_resources.h"
|
|
5
|
+
#include "rug_events.h"
|
|
6
|
+
|
|
7
|
+
#include <SDL/SDL.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <stdio.h>
|
|
10
|
+
|
|
11
|
+
SDL_Surface * mainWnd = NULL;
|
|
12
|
+
VALUE loadFunc, drawFunc, updateFunc;
|
|
13
|
+
|
|
14
|
+
VALUE mRug, mRugEvent;
|
|
15
|
+
|
|
16
|
+
extern _RugConf RugConf;
|
|
17
|
+
|
|
18
|
+
static VALUE RugLoad(int argc, VALUE * argv, VALUE class){
|
|
19
|
+
if (rb_block_given_p()){
|
|
20
|
+
VALUE func;
|
|
21
|
+
rb_scan_args(argc, argv, "0&", &func);
|
|
22
|
+
loadFunc = func;
|
|
23
|
+
}
|
|
24
|
+
return Qnil;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static VALUE RugDraw(int argc, VALUE * argv, VALUE class){
|
|
28
|
+
if (rb_block_given_p()){
|
|
29
|
+
VALUE func;
|
|
30
|
+
rb_scan_args(argc, argv, "0&", &func);
|
|
31
|
+
drawFunc = func;
|
|
32
|
+
}
|
|
33
|
+
return Qnil;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static VALUE RugUpdate(int argc, VALUE * argv, VALUE class){
|
|
37
|
+
if (rb_block_given_p()){
|
|
38
|
+
VALUE func;
|
|
39
|
+
rb_scan_args(argc, argv, "0&", &func);
|
|
40
|
+
updateFunc = func;
|
|
41
|
+
}
|
|
42
|
+
return Qnil;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static VALUE RugStart(VALUE class){
|
|
46
|
+
SDL_Init(SDL_INIT_VIDEO);
|
|
47
|
+
atexit(SDL_Quit);
|
|
48
|
+
|
|
49
|
+
SDL_Event ev;
|
|
50
|
+
int lastDraw = 0;
|
|
51
|
+
|
|
52
|
+
int frameGap = RugConf.frameGap;
|
|
53
|
+
|
|
54
|
+
mainWnd = DoConf();
|
|
55
|
+
|
|
56
|
+
if (loadFunc != Qnil){
|
|
57
|
+
rb_funcall(loadFunc, rb_intern("call"), 0);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// TODO: make background configurable
|
|
61
|
+
Uint32 black = SDL_MapRGB(mainWnd->format, 0, 0, 0);
|
|
62
|
+
|
|
63
|
+
while (1){
|
|
64
|
+
if (SDL_PollEvent(&ev)){
|
|
65
|
+
if (!HandleEvent(ev)){
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
int now = SDL_GetTicks();
|
|
71
|
+
if (lastDraw + frameGap < now){
|
|
72
|
+
// update
|
|
73
|
+
if (updateFunc != Qnil){
|
|
74
|
+
rb_funcall(updateFunc, rb_intern("call"), 1, INT2NUM(now - lastDraw));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// render
|
|
78
|
+
SDL_FillRect(mainWnd, NULL, black);
|
|
79
|
+
|
|
80
|
+
if (drawFunc != Qnil){
|
|
81
|
+
rb_funcall(drawFunc, rb_intern("call"), 0);
|
|
82
|
+
}
|
|
83
|
+
SDL_UpdateRect(mainWnd, 0, 0, 0, 0);
|
|
84
|
+
lastDraw = now;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
SDL_Delay(1);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return Qnil;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
VALUE RugShowCursor(VALUE class, VALUE show){
|
|
94
|
+
SDL_ShowCursor(TYPE(show) == T_TRUE);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
void Init_Rug(){
|
|
98
|
+
loadFunc = updateFunc = drawFunc = Qnil;
|
|
99
|
+
|
|
100
|
+
// Main Rug module
|
|
101
|
+
mRug = rb_define_module("Rug");
|
|
102
|
+
|
|
103
|
+
// main Rug actions
|
|
104
|
+
rb_define_singleton_method(mRug, "load", RugLoad, -1);
|
|
105
|
+
rb_define_singleton_method(mRug, "draw", RugDraw, -1);
|
|
106
|
+
rb_define_singleton_method(mRug, "update", RugUpdate, -1);
|
|
107
|
+
rb_define_singleton_method(mRug, "start", RugStart, -1);
|
|
108
|
+
|
|
109
|
+
// some basic options
|
|
110
|
+
rb_define_singleton_method(mRug, "show_cursor=", RugShowCursor, 1);
|
|
111
|
+
|
|
112
|
+
// load additional classes/modules
|
|
113
|
+
LoadConf(mRug);
|
|
114
|
+
LoadEvents(mRug);
|
|
115
|
+
LoadResourcesModule(mRug);
|
|
116
|
+
LoadLayer(mRug);
|
|
117
|
+
}
|
data/ext/rug_conf.c
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#include "rug_conf.h"
|
|
2
|
+
|
|
3
|
+
#include <SDL/SDL.h>
|
|
4
|
+
|
|
5
|
+
_RugConf RugConf;
|
|
6
|
+
VALUE cRugConf;
|
|
7
|
+
VALUE block_converter;
|
|
8
|
+
|
|
9
|
+
VALUE RugConfSetWidth(VALUE self, VALUE _width){
|
|
10
|
+
RugConf.width = NUM2INT(_width);
|
|
11
|
+
return _width;
|
|
12
|
+
}
|
|
13
|
+
VALUE RugConfSetHeight(VALUE self, VALUE _height){
|
|
14
|
+
RugConf.height = NUM2INT(_height);
|
|
15
|
+
return _height;
|
|
16
|
+
}
|
|
17
|
+
VALUE RugConfSetBpp(VALUE self, VALUE _bpp){
|
|
18
|
+
RugConf.bpp = NUM2INT(_bpp);
|
|
19
|
+
return _bpp;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
VALUE RugConfSetTitle(VALUE self, VALUE _title){
|
|
23
|
+
RugConf.title = _title;
|
|
24
|
+
return _title;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
VALUE RugConfSetDelay(VALUE self, VALUE _repeatDelay){
|
|
28
|
+
RugConf.repeatDelay = _repeatDelay;
|
|
29
|
+
return _repeatDelay;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
VALUE RugConfSetInterval(VALUE self, VALUE _repeatInterval){
|
|
33
|
+
RugConf.repeatInterval = _repeatInterval;
|
|
34
|
+
return _repeatInterval;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
VALUE RugConfSetFullscreen(VALUE self, VALUE _fullscreen){
|
|
38
|
+
RugConf.fullscreen = (TYPE(_fullscreen) == T_TRUE ? 1 : 0);
|
|
39
|
+
return _fullscreen;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
VALUE RugConfSetFPS(VALUE self, VALUE _fps){
|
|
43
|
+
RugConf.frameGap = 1000 / _fps;
|
|
44
|
+
return _fps;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static VALUE RugConfFunc(int argc, VALUE * argv, VALUE class){
|
|
48
|
+
// we keep track of the RugConf block and instance_eval it within
|
|
49
|
+
// a RugConf object
|
|
50
|
+
if (rb_block_given_p()){
|
|
51
|
+
VALUE confFunc;
|
|
52
|
+
rb_scan_args(argc, argv, "0&", &confFunc);
|
|
53
|
+
VALUE confObj = rb_funcall(cRugConf, rb_intern("new"), 0);
|
|
54
|
+
rb_funcall(block_converter, rb_intern("call"), 3, confObj, rb_str_new2("instance_eval"), confFunc);
|
|
55
|
+
}
|
|
56
|
+
return Qnil;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// This function performs all configuration necessary and returns the screen object
|
|
60
|
+
SDL_Surface * DoConf(){
|
|
61
|
+
int params = SDL_HWSURFACE;
|
|
62
|
+
|
|
63
|
+
if (RugConf.fullscreen){
|
|
64
|
+
params |= SDL_FULLSCREEN;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
SDL_Surface * wnd = SDL_SetVideoMode(RugConf.width, RugConf.height, RugConf.bpp, params);
|
|
68
|
+
if (RugConf.title != Qnil){
|
|
69
|
+
SDL_WM_SetCaption(STR2CSTR(RugConf.title), NULL);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// enable key repeating
|
|
73
|
+
SDL_EnableKeyRepeat(RugConf.repeatDelay, RugConf.repeatInterval);
|
|
74
|
+
|
|
75
|
+
return wnd;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
void LoadConf(VALUE mRug){
|
|
79
|
+
// Set some defaults
|
|
80
|
+
RugConf.title = Qnil;
|
|
81
|
+
RugConf.width = 800;
|
|
82
|
+
RugConf.height = 600;
|
|
83
|
+
RugConf.bpp = 32;
|
|
84
|
+
RugConf.frameGap = 33;
|
|
85
|
+
RugConf.fullscreen = 0;
|
|
86
|
+
RugConf.repeatDelay = SDL_DEFAULT_REPEAT_DELAY;
|
|
87
|
+
RugConf.repeatInterval = SDL_DEFAULT_REPEAT_INTERVAL;
|
|
88
|
+
|
|
89
|
+
block_converter = rb_eval_string("proc { |recv, msg, block| recv.send(msg, &block) }");
|
|
90
|
+
// add to main module
|
|
91
|
+
rb_define_singleton_method(mRug, "conf", RugConfFunc, -1);
|
|
92
|
+
|
|
93
|
+
// create conf class
|
|
94
|
+
cRugConf = rb_define_class_under(mRug, "Conf", rb_cObject);
|
|
95
|
+
|
|
96
|
+
rb_define_method(cRugConf, "width", RugConfSetWidth, 1);
|
|
97
|
+
rb_define_method(cRugConf, "height", RugConfSetHeight, 1);
|
|
98
|
+
rb_define_method(cRugConf, "bpp", RugConfSetBpp, 1);
|
|
99
|
+
rb_define_method(cRugConf, "title", RugConfSetTitle, 1);
|
|
100
|
+
rb_define_method(cRugConf, "fps", RugConfSetFPS, 1);
|
|
101
|
+
rb_define_method(cRugConf, "fullscreen", RugConfSetFullscreen, 1);
|
|
102
|
+
rb_define_method(cRugConf, "key_repeat_delay", RugConfSetDelay, 1);
|
|
103
|
+
rb_define_method(cRugConf, "key_repeat_interval", RugConfSetInterval, 1);
|
|
104
|
+
}
|
data/ext/rug_conf.h
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#ifndef RUG_CONF_H
|
|
2
|
+
#define RUG_CONF_H
|
|
3
|
+
|
|
4
|
+
#include "ruby.h"
|
|
5
|
+
#include <SDL/SDL.h>
|
|
6
|
+
|
|
7
|
+
typedef struct {
|
|
8
|
+
int width, height, bpp;
|
|
9
|
+
VALUE title;
|
|
10
|
+
int repeatDelay, repeatInterval;
|
|
11
|
+
int frameGap;
|
|
12
|
+
int fullscreen;
|
|
13
|
+
} _RugConf;
|
|
14
|
+
|
|
15
|
+
void LoadConf(VALUE);
|
|
16
|
+
SDL_Surface * DoConf();
|
|
17
|
+
|
|
18
|
+
#endif //RUG_CONF_H
|
data/ext/rug_events.c
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
#include "rug_events.h"
|
|
2
|
+
|
|
3
|
+
#include <SDL/SDL.h>
|
|
4
|
+
|
|
5
|
+
struct {
|
|
6
|
+
VALUE KeyUp, KeyDown, MouseMove, MouseDown, MouseUp, MouseMoveOffset;
|
|
7
|
+
} RugEvents;
|
|
8
|
+
|
|
9
|
+
static VALUE SetKeyUp(int argc, VALUE * argv, VALUE self){
|
|
10
|
+
VALUE keyup;
|
|
11
|
+
rb_scan_args(argc, argv, "0&", &keyup);
|
|
12
|
+
RugEvents.KeyUp = keyup;
|
|
13
|
+
return Qnil;
|
|
14
|
+
}
|
|
15
|
+
static VALUE SetKeyDown(int argc, VALUE * argv, VALUE self){
|
|
16
|
+
VALUE keydown;
|
|
17
|
+
rb_scan_args(argc, argv, "0&", &keydown);
|
|
18
|
+
RugEvents.KeyDown = keydown;
|
|
19
|
+
return Qnil;
|
|
20
|
+
}
|
|
21
|
+
static VALUE SetMouseMove(int argc, VALUE * argv, VALUE self){
|
|
22
|
+
VALUE mousemove;
|
|
23
|
+
rb_scan_args(argc, argv, "0&", &mousemove);
|
|
24
|
+
RugEvents.MouseMove = mousemove;
|
|
25
|
+
return Qnil;
|
|
26
|
+
}
|
|
27
|
+
static VALUE SetMouseDown(int argc, VALUE * argv, VALUE self){
|
|
28
|
+
VALUE mousedown;
|
|
29
|
+
rb_scan_args(argc, argv, "0&", &mousedown);
|
|
30
|
+
RugEvents.MouseDown = mousedown;
|
|
31
|
+
return Qnil;
|
|
32
|
+
}
|
|
33
|
+
static VALUE SetMouseUp(int argc, VALUE * argv, VALUE self){
|
|
34
|
+
VALUE mouseup;
|
|
35
|
+
rb_scan_args(argc, argv, "0&", &mouseup);
|
|
36
|
+
RugEvents.MouseUp = mouseup;
|
|
37
|
+
return Qnil;
|
|
38
|
+
}
|
|
39
|
+
static VALUE SetMouseMoveOffset(int argc, VALUE * argv, VALUE self){
|
|
40
|
+
VALUE func;
|
|
41
|
+
rb_scan_args(argc, argv, "0&", &func);
|
|
42
|
+
RugEvents.MouseMoveOffset = func;
|
|
43
|
+
return Qnil;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
void LoadEvents(VALUE mRug){
|
|
47
|
+
rb_define_singleton_method(mRug, "keyup", SetKeyUp, -1);
|
|
48
|
+
rb_define_singleton_method(mRug, "keydown", SetKeyDown, -1);
|
|
49
|
+
rb_define_singleton_method(mRug, "mousemove", SetMouseMove, -1);
|
|
50
|
+
rb_define_singleton_method(mRug, "mousemoveoffset", SetMouseMoveOffset, -1);
|
|
51
|
+
rb_define_singleton_method(mRug, "mousedown", SetMouseDown, -1);
|
|
52
|
+
rb_define_singleton_method(mRug, "mouseup", SetMouseUp, -1);
|
|
53
|
+
|
|
54
|
+
VALUE mKeyModule = rb_define_module_under(mRug, "Key");
|
|
55
|
+
VALUE mMouseModule = rb_define_module_under(mRug, "Mouse");
|
|
56
|
+
|
|
57
|
+
rb_define_const(mKeyModule, "Left", INT2FIX(SDLK_LEFT));
|
|
58
|
+
rb_define_const(mKeyModule, "Right", INT2FIX(SDLK_RIGHT));
|
|
59
|
+
rb_define_const(mKeyModule, "Down", INT2FIX(SDLK_DOWN));
|
|
60
|
+
rb_define_const(mKeyModule, "Up", INT2FIX(SDLK_UP));
|
|
61
|
+
// TODO: all the other characters
|
|
62
|
+
|
|
63
|
+
// load in letters and numbers
|
|
64
|
+
char buff[3];
|
|
65
|
+
buff[1] = '\0';
|
|
66
|
+
for (buff[0] = 'A'; buff[0] <= 'Z'; buff[0]++){
|
|
67
|
+
rb_define_const(mKeyModule, buff, INT2FIX(buff[0] | 0x20));
|
|
68
|
+
}
|
|
69
|
+
buff[0] = 'K';
|
|
70
|
+
buff[2] = '\0';
|
|
71
|
+
for (buff[1] = '0'; buff[1] <= '9'; buff[1]++){
|
|
72
|
+
rb_define_const(mKeyModule, buff, INT2FIX(buff[1]));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
rb_define_const(mMouseModule, "Left", INT2FIX(SDL_BUTTON_LEFT));
|
|
76
|
+
rb_define_const(mMouseModule, "Middle", INT2FIX(SDL_BUTTON_MIDDLE));
|
|
77
|
+
rb_define_const(mMouseModule, "Right", INT2FIX(SDL_BUTTON_RIGHT));
|
|
78
|
+
|
|
79
|
+
RugEvents.KeyUp = Qnil;
|
|
80
|
+
RugEvents.KeyDown = Qnil;
|
|
81
|
+
RugEvents.MouseMove = Qnil;
|
|
82
|
+
RugEvents.MouseDown = Qnil;
|
|
83
|
+
RugEvents.MouseUp = Qnil;
|
|
84
|
+
RugEvents.MouseMoveOffset = Qnil;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
int HandleEvent(SDL_Event ev){
|
|
88
|
+
switch(ev.type){
|
|
89
|
+
case SDL_KEYUP:
|
|
90
|
+
if (RugEvents.KeyUp != Qnil){
|
|
91
|
+
rb_funcall(RugEvents.KeyUp, rb_intern("call"), 1, INT2FIX(ev.key.keysym.sym));
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
case SDL_KEYDOWN:
|
|
95
|
+
if (RugEvents.KeyDown != Qnil){
|
|
96
|
+
rb_funcall(RugEvents.KeyDown, rb_intern("call"), 1, INT2FIX(ev.key.keysym.sym));
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
case SDL_MOUSEMOTION:
|
|
100
|
+
if (RugEvents.MouseMove != Qnil){
|
|
101
|
+
rb_funcall(RugEvents.MouseMove, rb_intern("call"), 2, INT2FIX(ev.motion.x), INT2FIX(ev.motion.y));
|
|
102
|
+
}
|
|
103
|
+
if (RugEvents.MouseMoveOffset != Qnil){
|
|
104
|
+
rb_funcall(RugEvents.MouseMoveOffset, rb_intern("call"), 2, INT2FIX(ev.motion.xrel), INT2FIX(ev.motion.yrel));
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
case SDL_MOUSEBUTTONDOWN:
|
|
108
|
+
if (RugEvents.MouseDown != Qnil){
|
|
109
|
+
rb_funcall(RugEvents.MouseDown, rb_intern("call"), 3, INT2FIX(ev.button.x), INT2FIX(ev.button.y), INT2FIX(ev.button.button));
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
case SDL_MOUSEBUTTONUP:
|
|
113
|
+
if (RugEvents.MouseUp != Qnil){
|
|
114
|
+
rb_funcall(RugEvents.MouseUp, rb_intern("call"), 3, INT2FIX(ev.button.x), INT2FIX(ev.button.y), INT2FIX(ev.button.button));
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
case SDL_QUIT:
|
|
118
|
+
return 0;
|
|
119
|
+
default:
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return 1;
|
|
124
|
+
}
|
data/ext/rug_events.h
ADDED
data/ext/rug_layer.c
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#include "rug_layer.h"
|
|
2
|
+
|
|
3
|
+
VALUE cRugLayer;
|
|
4
|
+
|
|
5
|
+
extern SDL_Surface * mainWnd;
|
|
6
|
+
|
|
7
|
+
void ClearLayer(RugLayer * rLayer){
|
|
8
|
+
Uint32 clear = SDL_MapRGBA(rLayer->layer->format, 0, 0, 0, 0);
|
|
9
|
+
SDL_FillRect(rLayer->layer, NULL, clear);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static void unload_layer(void * vp){
|
|
13
|
+
RugLayer * rLayer = (RugLayer *)vp;
|
|
14
|
+
SDL_FreeSurface(rLayer->layer);
|
|
15
|
+
rLayer->layer = NULL;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static VALUE RugInit(VALUE class){
|
|
19
|
+
if (mainWnd == NULL){
|
|
20
|
+
return Qnil;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
RugLayer * rLayer = ALLOC(RugLayer);
|
|
24
|
+
|
|
25
|
+
rLayer->layer = SDL_CreateRGBSurface(SDL_HWSURFACE, mainWnd->w, mainWnd->h, mainWnd->format->BitsPerPixel, 0, 0, 0, 0);
|
|
26
|
+
|
|
27
|
+
// make the surface transparent
|
|
28
|
+
ClearLayer(rLayer);
|
|
29
|
+
|
|
30
|
+
return Data_Wrap_Struct(cRugLayer, NULL, unload_layer, rLayer);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static VALUE RugDrawLayer(VALUE self){
|
|
34
|
+
RugLayer * rLayer;
|
|
35
|
+
Data_Get_Struct(self, RugLayer, rLayer);
|
|
36
|
+
SDL_BlitSurface(rLayer->layer, NULL, mainWnd, NULL);
|
|
37
|
+
return Qnil;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static VALUE RugClearLayer(VALUE self){
|
|
41
|
+
RugLayer * rLayer;
|
|
42
|
+
Data_Get_Struct(self, RugLayer, rLayer);
|
|
43
|
+
ClearLayer(rLayer);
|
|
44
|
+
return Qnil;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
void LoadLayer(VALUE mRug){
|
|
48
|
+
// create conf class
|
|
49
|
+
cRugLayer = rb_define_class_under(mRug, "Layer", rb_cObject);
|
|
50
|
+
|
|
51
|
+
rb_define_singleton_method(cRugLayer, "new", RugInit, 0);
|
|
52
|
+
rb_define_method(cRugLayer, "draw", RugDrawLayer, 0);
|
|
53
|
+
rb_define_method(cRugLayer, "clear", RugClearLayer, 0);
|
|
54
|
+
}
|
data/ext/rug_layer.h
ADDED
data/ext/rug_resources.c
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#include "rug_resources.h"
|
|
2
|
+
#include "rug_layer.h"
|
|
3
|
+
|
|
4
|
+
#include <SDL/SDL_image.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
|
|
8
|
+
VALUE cRugImage;
|
|
9
|
+
|
|
10
|
+
extern SDL_Surface * mainWnd;
|
|
11
|
+
|
|
12
|
+
typedef struct {
|
|
13
|
+
SDL_Surface * image;
|
|
14
|
+
} RugImage;
|
|
15
|
+
|
|
16
|
+
static void unload_image(void * vp){
|
|
17
|
+
RugImage * rImage = (RugImage *)vp;
|
|
18
|
+
SDL_FreeSurface(rImage->image);
|
|
19
|
+
rImage->image = NULL;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static VALUE new_image(VALUE class, VALUE filename){
|
|
23
|
+
SDL_Surface * image = IMG_Load(STR2CSTR(filename));
|
|
24
|
+
|
|
25
|
+
if (!image){
|
|
26
|
+
// throw exception
|
|
27
|
+
rb_raise(rb_eIOError, "Unable to load image!");
|
|
28
|
+
}else{
|
|
29
|
+
RugImage * rImage = ALLOC(RugImage);
|
|
30
|
+
|
|
31
|
+
rImage->image = image;
|
|
32
|
+
|
|
33
|
+
return Data_Wrap_Struct(cRugImage, NULL, unload_image, rImage);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return Qnil;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static VALUE blit_image(int argc, VALUE * argv, VALUE self){
|
|
40
|
+
if (mainWnd != NULL){
|
|
41
|
+
VALUE sx, sy, x, y, width, height, targetLayer;
|
|
42
|
+
|
|
43
|
+
rb_scan_args(argc, argv, "25", &x, &y, &width, &height, &sx, &sy, &targetLayer);
|
|
44
|
+
|
|
45
|
+
RugImage * image;
|
|
46
|
+
Data_Get_Struct(self, RugImage, image);
|
|
47
|
+
|
|
48
|
+
SDL_Rect src, dst;
|
|
49
|
+
|
|
50
|
+
SDL_Surface * target;
|
|
51
|
+
|
|
52
|
+
if (targetLayer == Qnil){
|
|
53
|
+
target = mainWnd;
|
|
54
|
+
}else{
|
|
55
|
+
RugLayer * layer;
|
|
56
|
+
Data_Get_Struct(targetLayer, RugLayer, layer);
|
|
57
|
+
target = layer->layer;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
dst.x = FIX2INT(x);
|
|
61
|
+
dst.y = FIX2INT(y);
|
|
62
|
+
dst.w = dst.h = 0;
|
|
63
|
+
|
|
64
|
+
if (width != Qnil){
|
|
65
|
+
src.w = FIX2INT(width);
|
|
66
|
+
|
|
67
|
+
if (height != Qnil){
|
|
68
|
+
src.h = FIX2INT(height);
|
|
69
|
+
}else{
|
|
70
|
+
src.h = src.w;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (x != Qnil){
|
|
74
|
+
src.x = FIX2INT(sx);
|
|
75
|
+
}else{
|
|
76
|
+
src.x = 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (y != Qnil){
|
|
80
|
+
src.y = FIX2INT(sy);
|
|
81
|
+
}else{
|
|
82
|
+
src.y = 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
SDL_BlitSurface(image->image, &src, target, &dst);
|
|
86
|
+
}else{
|
|
87
|
+
SDL_BlitSurface(image->image, NULL, target, &dst);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return self;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
void LoadResourcesModule(VALUE rugModule){
|
|
95
|
+
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF);
|
|
96
|
+
atexit(IMG_Quit);
|
|
97
|
+
|
|
98
|
+
// define Image class
|
|
99
|
+
cRugImage = rb_define_class_under(rugModule, "Image", rb_cObject);
|
|
100
|
+
|
|
101
|
+
rb_define_singleton_method(cRugImage, "new", new_image, 1);
|
|
102
|
+
rb_define_method(cRugImage, "draw", blit_image, -1);
|
|
103
|
+
}
|
|
104
|
+
|
data/ext/rug_resources.h
ADDED
data/lib/Rug.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'Rug.so'
|
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: Rug
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 27
|
|
5
|
+
prerelease: false
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 0
|
|
9
|
+
- 2
|
|
10
|
+
version: 0.0.2
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- Rob Britton
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2010-11-21 00:00:00 -05:00
|
|
19
|
+
default_executable:
|
|
20
|
+
dependencies: []
|
|
21
|
+
|
|
22
|
+
description:
|
|
23
|
+
email: rob.s.brit@gmail.com
|
|
24
|
+
executables: []
|
|
25
|
+
|
|
26
|
+
extensions:
|
|
27
|
+
- ext/extconf.rb
|
|
28
|
+
extra_rdoc_files: []
|
|
29
|
+
|
|
30
|
+
files:
|
|
31
|
+
- Rakefile
|
|
32
|
+
- lib/Rug.rb
|
|
33
|
+
- ext/rug_resources.c
|
|
34
|
+
- ext/rug_events.c
|
|
35
|
+
- ext/rug_layer.c
|
|
36
|
+
- ext/rug_conf.c
|
|
37
|
+
- ext/rug.c
|
|
38
|
+
- ext/rug_conf.h
|
|
39
|
+
- ext/rug_resources.h
|
|
40
|
+
- ext/rug_events.h
|
|
41
|
+
- ext/rug_layer.h
|
|
42
|
+
- README
|
|
43
|
+
- ext/extconf.rb
|
|
44
|
+
has_rdoc: true
|
|
45
|
+
homepage:
|
|
46
|
+
licenses: []
|
|
47
|
+
|
|
48
|
+
post_install_message:
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
|
|
51
|
+
require_paths:
|
|
52
|
+
- lib
|
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
|
+
none: false
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
hash: 3
|
|
59
|
+
segments:
|
|
60
|
+
- 0
|
|
61
|
+
version: "0"
|
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
|
+
none: false
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
hash: 3
|
|
68
|
+
segments:
|
|
69
|
+
- 0
|
|
70
|
+
version: "0"
|
|
71
|
+
requirements: []
|
|
72
|
+
|
|
73
|
+
rubyforge_project:
|
|
74
|
+
rubygems_version: 1.3.7
|
|
75
|
+
signing_key:
|
|
76
|
+
specification_version: 3
|
|
77
|
+
summary: A simple game framework.
|
|
78
|
+
test_files: []
|
|
79
|
+
|