Commit 310caeec authored by David Quiroga's avatar David Quiroga
Browse files

Initial commit

parents
This diff is collapsed.
# Install fonts
install_data(
[
'fonts/JetMono-EB.ttf',
],
install_dir: join_paths(get_option('datadir'), meson.project_name(), 'fonts')
)
project('shapecollision', ['c', 'vala'],
version: '0.1.0',
meson_version: '>= 0.50.0',
default_options: [ 'warning_level=2',
],
)
# Set gettext package name
add_global_arguments('-DGETTEXT_PACKAGE="@0@"'.format (meson.project_name()), language: 'c')
localedir = join_paths(get_option('prefix'), get_option('localedir'))
datadir = join_paths(get_option('prefix'), get_option('datadir'), meson.project_name())
fontsdir = join_paths(datadir, 'fonts')
conf = configuration_data ()
conf.set_quoted ('VERSION', meson.project_version())
conf.set_quoted ('GETTEXT_PACKAGE', meson.project_name())
conf.set_quoted ('FONTS_DIRECTORY', fontsdir)
conf.set_quoted ('LOCALEDIR', localedir)
configure_file(output: 'config.h', configuration: conf)
config_h_dir = include_directories('.')
subdir('data')
subdir('src')
/* main.vala
*
* Copyright 2021 David Quiroga
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
int main (string[] args)
{
print ("Loading SDL...\n");
var gameloop = new GSDL.GameLoop ();
gameloop.run ();
return 0;
}
app_sources = [
'vapi/config.vapi',
'main.vala',
'sdl/wsdl.vala',
'sdl/gameloop.vala',
'sdl/input.vala',
'sdl/text.vala',
]
app_deps = [
dependency('glib-2.0'),
dependency('gobject-2.0'),
dependency('gio-2.0'),
dependency ('gee-0.8'),
dependency('sdl2'),
dependency('SDL2_gfx'),
dependency('sgp-0.1'),
dependency('SDL2_ttf'),
]
add_project_arguments([
'--vapidir', join_paths(meson.current_source_dir(), 'vapi'),
'--target-glib=2.50',
#'--save-temps',
],
language: 'vala'
)
executable('shapecollision', app_sources, dependencies: app_deps,
install: true,
c_args: [
'-include', 'config.h'
]
)
using SGP;
using Gee;
public class GSDL.GameLoop : Object {
private GSDL.Wsdl wsdl;
private GSDL.Input input;
private GSDL.Text text;
private GLib.Rand rand;
private IShape shape_c;
private Line2D line_c;
private Circle2D circle_c;
private Rectangle2D rectangle_c;
private OrientedRectangle2D oriented_rect_c;
private Line2D line1;
private Circle2D circle1;
private Rectangle2D rectangle1;
private OrientedRectangle2D oriented_rectangle1;
//private Geometry2D geo;
//private Line2D line2;
public GameLoop () {
wsdl = new GSDL.Wsdl ();
input = new GSDL.Input ();
text = new GSDL.Text (wsdl);
SGP.Utils.renderer = wsdl.renderer;
this.rand = new GLib.Rand ();
//geo = new Geometry2D ();
create_shapes ();
}
~GameLoop () {
SDL.quit ();
}
private void create_shapes () {
//SGP.Utils.smooth = false;
line_c = new Line2D (new Point2D (150, 300), new Point2D (250, 350));
//line1.rotate_center (13);
circle_c = new Circle2D (new Point2D (400, 400), 40);
rectangle_c = new Rectangle2D (new Point2D (100, 100),
new Point2D (150, 150));
oriented_rect_c = new OrientedRectangle2D (new Point2D (100, 100),
new Vector2D (25, 50), 00.0f);
//shape_c = circle_c;
// shape_c = oriented_rect_c;
//shape_c = rectangle_c;
shape_c = line_c;
line1 = new Line2D (new Point2D (100, 400), new Point2D (300, 400));
//line1.rotate_center (13);
circle1 = new Circle2D (new Point2D (400, 400), 40);
rectangle1 = new Rectangle2D (new Point2D (600, 350),
new Point2D (750, 450));
oriented_rectangle1 = new OrientedRectangle2D (new Point2D (1050, 400),
new Vector2D (100, 50), 00.0f);
//oriented_rectangle1.rotation = 13;
//line2 = new Line2D (new Point2D (1150, 371), new Point2D (1180, 500));
//geo = new Geometry2D ();
}
private void reset () {
var degress = rand.int_range (0, 360);
oriented_rectangle1.rotation += degress;
var radius = rand.int_range (20, 60);
circle1.radius = radius;
var v_x = rand.int_range (100, 250);
var v_y = rand.int_range (50, 150);
rectangle1.rsize = new Vector2D (v_x, v_y);
line1.rotate_center (degress);
//var colision = geo.line_oriented_rectangle (line2, oriented_rectangle1);
//print ("colision %s rotate %.05f\n", colision.to_string (), degress);
}
private void reset_cursor () {
var degress = rand.int_range (0, 360);
oriented_rect_c.rotation += degress;
var radius = rand.int_range (20, 60);
circle_c.radius = radius;
var v_x = rand.int_range (100, 250);
var v_y = rand.int_range (50, 150);
rectangle_c.rsize = new Vector2D (v_x, v_y);
line_c.rotate_center (degress);
var nshape = rand.int_range (0, 4);
switch (nshape) {
case 0: shape_c = line_c;
break;
case 1: shape_c = circle_c;
break;
case 2: shape_c = rectangle_c;
break;
default:shape_c = oriented_rect_c;
break;
}
}
public void run () {
wsdl.init_loop ();
while (!input.done) {
wsdl.prepare_scene ();
input.process_events ();
stage_run ();
wsdl.present_scene ();
wsdl.cap_frame_rate ();
}
}
private void stage_run () {
if (input.mouse_button_1 == true)
reset ();
if (input.mouse_button_3 == true)
reset_cursor ();
text.draw_click_for_reset ();
move ();
draw ();
}
private void move () {
Point2D cursor = new Point2D.vect (input.vmouse);
shape_c.position = cursor;
if (shape_c.collision (line1)) {
line1.set_rgba_color (80, 22, 10, 255);
} else {
line1.set_rgba_color (255, 255, 255, 255);
}
if (shape_c.collision (circle1)) {
circle1.set_rgba_color (80, 22, 10, 255);
} else {
circle1.set_rgba_color (255, 255, 255, 255);
}
if (shape_c.collision (rectangle1)) {
rectangle1.set_rgba_color (80, 22, 10, 255);
} else {
rectangle1.set_rgba_color (255, 255, 255, 255);
}
if (shape_c.collision (oriented_rectangle1)) {
oriented_rectangle1.set_rgba_color (80, 22, 10, 255);
} else {
oriented_rectangle1.set_rgba_color (255, 255, 255, 255);
}
}
private void draw () {
line1.draw ();
rectangle1.draw ();
oriented_rectangle1.draw ();
circle1.draw ();
shape_c.draw ();
//line2.draw ();
//shape_c.draw ();
}
}
using SDL;
public class GSDL.Input : Object {
private const int MAX_KEYBOARD_KEYS = 350;
private bool[] _akeys = {};
public bool done { get; private set; default = false; }
public SGP.Point2D vmouse;
public bool mouse_button_1 { get; private set; default = false; }
public bool mouse_button_3 { get; private set; default = false; }
public Input () {
this._akeys = new bool[MAX_KEYBOARD_KEYS];
this.vmouse = new SGP.Point2D.cero ();
}
public bool[] get_akeys () {
return this._akeys;
}
public void set_akey (int key, bool state) {
if (key < MAX_KEYBOARD_KEYS)
this._akeys[key] = state;
}
public void process_events () {
Event event;
while (Event.poll (out event) != 0) {
switch (event.type) {
case EventType.QUIT:
this._done = true;
break;
case EventType.KEYDOWN:
this.on_keyboard_down (event.key);
break;
case EventType.KEYUP:
this.on_keyboard_up (event.key);
break;
case EventType.MOUSEMOTION:
this.mousemottion (event.motion);
break;
case EventType.MOUSEBUTTONDOWN:
this.mousebutton (event.button, true);
break;
case EventType.MOUSEBUTTONUP:
this.mousebutton (event.button, false);
break;
default:
break;
}
}
}
private void on_keyboard_up (KeyboardEvent event) {
if (event.repeat == 0 && event.keysym.scancode < MAX_KEYBOARD_KEYS) {
this._akeys[event.keysym.scancode] = false;
}
}
private void on_keyboard_down (KeyboardEvent event) {
if (event.repeat == 0 && event.keysym.scancode < MAX_KEYBOARD_KEYS) {
this._akeys[event.keysym.scancode] = true;
}
}
private void mousemottion (MouseMotionEvent event) {
this.vmouse.x = event.x;
this.vmouse.y = event.y;
}
private void mousebutton (MouseButtonEvent event, bool status) {
if (event.button == 1)
this.mouse_button_1 = status;
if (event.button == 3)
this.mouse_button_3 = status;
}
}
using SDL;
using SDLTTF;
public class GSDL.Text : Object {
private unowned Wsdl wsdl;
private Font font;
public Text (Wsdl wsdl) {
this.wsdl = wsdl;
string path = Path.build_filename (FONTS_DIRECTORY, "JetMono-EB.ttf");
if (wsdl.check_resource (path))
this.font = new Font (path, 30);
else
print ("Error loading fonts\n");
}
public void draw_click_for_reset () {
var lclick = "left click changes the shapes";
var rclick = "right click changes the cursor";
Video.Surface info_s = this.font
.render_blended_wrapped (rclick, {45,65,75,150}, 600);
var texture_info_s = Video.Texture.create_from_surface (wsdl.renderer,
info_s);
wsdl.renderer.copy (texture_info_s, {0, 0, info_s.w, info_s.h},
{40, 40, info_s.w, info_s.h });
info_s = this.font
.render_blended_wrapped (lclick, {45,65,75,150}, 600);
texture_info_s = Video.Texture.create_from_surface (wsdl.renderer,
info_s);
wsdl.renderer.copy (texture_info_s, {0, 0, info_s.w, info_s.h},
{40, 80, info_s.w, info_s.h });
}
}
using SDL;
using SDLGraphics;
public class GSDL.Wsdl : Object {
private const string window_name = "Shape Collision";
private const int DEFAULT_SCREEN_WIDTH = 1280;
private const int DEFAULT_SCREEN_HEIGHT = 720;
private const int FPS = 60;
private static int w_width = DEFAULT_SCREEN_WIDTH;
private static int w_height = DEFAULT_SCREEN_HEIGHT;
private uint32 _then = 0;
private float _remainder = 0;
private Video.Window window;
public Video.Renderer? renderer;
public Wsdl () {
if (SDL.init (SDL.InitFlag.VIDEO) != 0) {
critical ("Couldn't initialize SDL: %s\n", SDL.get_error());
}
SDLTTF.init();
SDL.Hint.set_hint_enabled (SDL.Hint.VIDEO_HIGHDPI_DISABLED, false);
window = new Video.Window (window_name,
(int) Video.Window.POS_CENTERED,
(int) Video.Window.POS_CENTERED,
width (), height (),
0);
renderer = Video.Renderer.create (window, -1,
Video.RendererFlags.ACCELERATED |
Video.RendererFlags.PRESENTVSYNC);
SDL.Input.Cursor.set_relative_mode (false);
SDL.Input.Cursor.show (0);
//window.set_fullscreen (SDL.Video.WindowFlags.FULLSCREEN_DESKTOP);
//SDL.Video.disable_screensaver ();
window.get_size (out w_width, out w_height);
print ("Window %dx%d\n", w_width, w_height);
assert (renderer != null);
}
public int width () {
return w_width;
}
public int height () {
return w_height;
}
public static int fps () {
return FPS;
}
public void init_loop () {
this._then = SDL.Timer.get_ticks ();
this._remainder = 0;
}
public void cap_frame_rate ()
{
float wait;
uint32 frameTime;
wait = 16 + this._remainder;
this._remainder -= (int)this._remainder;
frameTime = SDL.Timer.get_ticks () - this._then;
wait -= frameTime;
if (wait < 1)
wait = 1;
SDL.Timer.delay ((uint32) wait);
this._remainder += 0.667f;
this._then = SDL.Timer.get_ticks ();
}
public void prepare_scene () {
renderer.set_draw_color (32, 32, 32, 255);
renderer.clear();
}
public void present_scene () {
renderer.present ();
}
public bool check_resource (string path) {
var file = File.new_for_path (path);
if (!file.query_exists ()) {
warning ("Resource not found [%s]\n", path);
return false;
}
return true;
}
}
public const string VERSION;
public const string GETTEXT_PACKAGE;
public const string FONTS_DIRECTORY;
public const string LOCALEDIR;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment