Commit 11ef2e6a authored by David Quiroga's avatar David Quiroga
Browse files

Changed game loop

parent 394a4379
......@@ -5,9 +5,11 @@ public class GSDL.GameLoop : Object {
private GSDL.Wsdl wsdl;
private GSDL.Input input;
private GSDL.Text text;
private GLib.Rand rand;
private SDLTTF.Font jetmono;
private RFont msg_mouse_left;
private RFont msg_mouse_right;
private IShape shape_c;
private Line2D line_c;
private Circle2D circle_c;
......@@ -22,17 +24,22 @@ public class GSDL.GameLoop : Object {
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 ();
create_shapes ();
load_resources ();
create_messages ();
}
~GameLoop () {
SDL.quit ();
}
private void create_shapes () {
private void load_resources () {
jetmono = RFont.load_font_file ("JetMono-EB.ttf", 30);
}
private void create_messages () {
msg_mouse_left = new RFont (wsdl, jetmono);
msg_mouse_right = new RFont (wsdl, jetmono);
//SGP.Utils.smooth = false;
line_c = new Line2D (new Point2D (150, 300), new Point2D (250, 350));
circle_c = new Circle2D (new Point2D (400, 400), 40);
......@@ -52,25 +59,25 @@ public class GSDL.GameLoop : Object {
}
private void reset () {
var degress = rand.int_range (0, 360);
oriented_rectangle1.rotation += degress;
var degrees = rand.int_range (0, 360);
oriented_rectangle1.rotation += degrees;
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);
line1.rotate_center (degrees);
}
private void reset_cursor () {
var degress = rand.int_range (0, 360);
oriented_rect_c.rotation += degress;
var degrees = rand.int_range (0, 360);
oriented_rect_c.rotation += degrees;
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);
line_c.rotate_center (degrees);
var nshape = rand.int_range (0, 4);
switch (nshape) {
......@@ -86,32 +93,34 @@ public class GSDL.GameLoop : Object {
}
public void run () {
wsdl.init_loop ();
uint32 previous = wsdl.get_time ();
double lag = 0.0f;
while (!input.done) {
uint32 current = wsdl.get_time ();
uint32 elapsed = current - previous;
previous = current;
lag += elapsed;
wsdl.prepare_scene ();
input.process_events ();
stage_run ();
while (lag >= Wsdl.MS_PER_UPDATE) {
update ();
lag -= Wsdl.MS_PER_UPDATE;
}
draw ();
wsdl.present_scene ();
wsdl.cap_frame_rate ();
wsdl.wait (1);
}
}
private void stage_run () {
private void update () {
msg_mouse_left.set_text_blended ("left click changes the shapes", 600);
msg_mouse_right.set_text_blended ("right click changes the cursor", 600);
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.copy ();
......@@ -139,6 +148,8 @@ public class GSDL.GameLoop : Object {
}
private void draw () {
msg_mouse_left.write (40, 40);
msg_mouse_right.write (40, 80);
line1.draw ();
rectangle1.draw ();
oriented_rectangle1.draw ();
......
......@@ -2,9 +2,9 @@ app_sources = [
'vapi/config.vapi',
'main.vala',
'sdl/wsdl.vala',
'sdl/gameloop.vala',
'sdl/rfont.vala',
'sdl/input.vala',
'sdl/text.vala',
'gameloop.vala',
]
app_deps = [
......
using SDL;
using SDL.Video;
using SDLTTF;
public class GSDL.RFont
{
private unowned Wsdl wsdl;
private unowned Font font;
public Color color { get; set; default = { 96, 97, 98, 250 }; }
public Color background_color { get; set; default = { 96, 97, 98, 150 }; }
private Video.Surface info_s;
private Texture? texture_info_s = null;
public RFont (Wsdl wsdl, Font font) {
this.wsdl = wsdl;
this.font = font;
}
public static Font load_font_file (string file, int size = 20) {
Font ret_font;
string path = Path.build_filename (FONTS_DIRECTORY, file);
check_resource (path);
ret_font = new Font (path, size);
assert (ret_font != null);
return ret_font;
}
private static void check_resource (string path) {
var file = File.new_for_path (path);
if (!file.query_exists ()) {
critical ("Resource not found [%s]\n", path);
}
}
public void set_text_solid (string text = "Demo") {
info_s = this.font.render (text, this._color);
texture_info_s = Video.Texture.create_from_surface (wsdl.renderer,
info_s);
}
public void set_text_shaded (string text = "Demo") {
info_s = this.font.render_shaded (text, this._color,
this._background_color);
texture_info_s = Video.Texture.create_from_surface (wsdl.renderer,
info_s);
}
public void set_text_blended (string text = "Demo", int wrap_length = 200) {
info_s = this.font
.render_blended_wrapped (text, this._color, wrap_length);
texture_info_s = Video.Texture.create_from_surface (wsdl.renderer,
info_s);
}
public void write (int position_x, int position_y) {
if (this.texture_info_s == null)
set_text_solid ();
wsdl.renderer.copy (texture_info_s, {0, 0, info_s.w, info_s.h},
{position_x, position_y, info_s.w, info_s.h });
}
public void print_metric (uchar letter) {
int minx, maxx, miny, maxy, advance, res;
res = this.font.get_metrics (letter, out minx, out maxx, out miny,
out maxy, out advance);
if (res == -1) {
warning ("Error in get_metrics: %s\n", SDL.get_error ());
} else {
int fontheight = this.font.get_height ();
int fontascent = this.font.get_ascent ();
int fontdescent = this.font.get_descent ();
int fontlineskip = this.font.get_lineskip ();
print ("minx\t\t:%d\n", minx);
print ("maxx\t\t:%d\n", maxx);
print ("miny\t\t:%d\n", miny);
print ("maxy\t\t:%d\n", maxy);
print ("advance\t\t:%d\n", advance);
print ("font height\t:%d\n", fontheight);
print ("font ascent\t:%d\n", fontascent);
print ("font descent\t:%d\n", fontdescent);
print ("font line skip\t:%d\n", fontlineskip);
}
}
}
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 });
}
}
......@@ -6,13 +6,11 @@ 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;
public const double MS_PER_UPDATE = 20;
private Video.Window window;
public Video.Renderer? renderer;
......@@ -22,7 +20,9 @@ public class GSDL.Wsdl : Object {
critical ("Couldn't initialize SDL: %s\n", SDL.get_error());
}
SDLTTF.init();
if (SDLTTF.init() == -1) {
critical ("Couldn't initialize SDLTTF: %s\n", SDL.get_error());
}
SDL.Hint.set_hint_enabled (SDL.Hint.VIDEO_HIGHDPI_DISABLED, false);
......@@ -47,6 +47,10 @@ public class GSDL.Wsdl : Object {
assert (renderer != null);
}
~Wsdl () {
SDL.quit ();
}
public int width () {
return w_width;
}
......@@ -55,34 +59,6 @@ public class GSDL.Wsdl : Object {
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();
......@@ -92,14 +68,12 @@ public class GSDL.Wsdl : Object {
renderer.present ();
}
public bool check_resource (string path) {
var file = File.new_for_path (path);
public uint32 get_time () {
return SDL.Timer.get_ticks ();
}
if (!file.query_exists ()) {
warning ("Resource not found [%s]\n", path);
return false;
}
return true;
public void wait (uint32 ms) {
SDL.Timer.delay (ms);
}
}
This diff is collapsed.
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2020 SDL2 VAPI Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Authors:
* Mario Daniel Ruiz Saavedra <desiderantes93@gmail.com>
* Gontzal Uriarte <txasatonga@gmail.com>
* Pedro H. Lara Campos <root@pedrohlc.com>
*/
[CCode (cheader_filename = "SDL2/SDL_image.h")]
namespace SDLImage {
//! Defines
[CCode (cname = "IMG_InitFlags", cprefix = "IMG_INIT_")]
public enum InitFlags {
JPG,
PNG,
TIF,
WEBP,
[CCode (cname = "IMG_INIT_JPG|IMG_INIT_PNG|IMG_INIT_TIF|IMG_INIT_WEBP")]
ALL
}
//! General
[CCode (cname = "IMG_Linked_Version")]
public static unowned SDL.Version? linked ();
[CCode (cname = "IMG_Init")]
public static int init (int flags);
[CCode (cname = "IMG_Quit")]
public static void quit ();
//! Loading
[CCode (cname = "IMG_Load")]
public static SDL.Video.Surface? load (string file);
[CCode (cname = "IMG_Load_RW")]
public static SDL.Video.Surface? load_rw (SDL.RWops src, bool freesrc = false);
[CCode (cname = "IMG_LoadTyped_RW")]
public static SDL.Video.Surface? load_rw_typed (SDL.RWops src, bool freesrc, string type);
[CCode (cname = "IMG_LoadTexture")]
public static SDL.Video.Texture? load_texture (SDL.Video.Renderer renderer, string file);
[CCode (cname = "IMG_LoadTexture_RW")]
public static SDL.Video.Texture? load_texture_rw (SDL.Video.Renderer renderer, SDL.RWops src, bool freesrc = false);
[CCode (cname = "IMG_LoadTextureTyped_RW")]
public static SDL.Video.Texture? load_texture_rw_typed (SDL.Video.Renderer renderer, SDL.RWops src, bool freesrc, string type);
[CCode (cname = "IMG_LoadCUR_RW")]
public static SDL.Video.Surface? load_cur (SDL.RWops src);
[CCode (cname = "IMG_LoadICO_RW")]
public static SDL.Video.Surface? load_ico (SDL.RWops src);
[CCode (cname = "IMG_LoadBMP_RW")]
public static SDL.Video.Surface? load_bmp (SDL.RWops src);
[CCode (cname = "IMG_LoadPNM_RW")]
public static SDL.Video.Surface? load_pnm (SDL.RWops src);
[CCode (cname = "IMG_LoadXPM_RW")]
public static SDL.Video.Surface? load_xpm (SDL.RWops src);
[CCode (cname = "IMG_LoadXCF_RW")]
public static SDL.Video.Surface? load_xcf (SDL.RWops src);
[CCode (cname = "IMG_LoadPCX_RW")]
public static SDL.Video.Surface? load_pcx (SDL.RWops src);
[CCode (cname = "IMG_LoadGIF_RW")]
public static SDL.Video.Surface? load_gif (SDL.RWops src);
[CCode (cname = "IMG_LoadJPG_RW")]
public static SDL.Video.Surface? load_jpg (SDL.RWops src);
[CCode (cname = "IMG_LoadTIF_RW")]
public static SDL.Video.Surface? load_tif (SDL.RWops src);
[CCode (cname = "IMG_LoadPNG_RW")]
public static SDL.Video.Surface? load_png (SDL.RWops src);
[CCode (cname = "IMG_LoadTGA_RW")]
public static SDL.Video.Surface? load_tga (SDL.RWops src);
[CCode (cname = "IMG_LoadLBM_RW")]
public static SDL.Video.Surface? load_lbm (SDL.RWops src);
[CCode (cname = "IMG_LoadXV_RW")]
public static SDL.Video.Surface? load_xv (SDL.RWops src);
[CCode (cname = "IMG_LoadWEBP_RW")]
public static SDL.Video.Surface? load_webp (SDL.RWops src);
[CCode (cname = "IMG_ReadXPMFromArray")]
public static SDL.Video.Surface? read_xpm ([CCode (array_length = false)] string[] xpmdata);
[CCode (cname = "IMG_SavePNG")]
public static int save_as_png (SDL.Video.Surface surface, string file);
[CCode (cname = "IMG_SavePNG_RW")]
public static int save_as_png_rw (SDL.Video.Surface surface, SDL.RWops dst, bool freedst);
[CCode (cname = "IMG_SaveJPG")]
public static int save_as_jpg (SDL.Video.Surface surface, string file, int quality);
[CCode (cname = "IMG_SaveJPG_RW")]
public static int save_as_jpg_rw (SDL.Video.Surface surface, SDL.RWops dst, bool freedst, int quality);
//!Info
[CCode (cname = "IMG_isCUR")]
public static bool is_cur (SDL.RWops src);
[CCode (cname = "IMG_isICO")]
public static bool is_ico (SDL.RWops src);
[CCode (cname = "IMG_isBMP")]
public static bool is_bmp (SDL.RWops src);
[CCode (cname = "IMG_isPNM")]
public static bool is_pnm (SDL.RWops src);
[CCode (cname = "IMG_isXPM")]
public static bool is_xpm (SDL.RWops src);
[CCode (cname = "IMG_isXCF")]
public static bool is_xcf (SDL.RWops src);
[CCode (cname = "IMG_isPCX")]
public static bool is_pcx (SDL.RWops src);
[CCode (cname = "IMG_isGIF")]
public static bool is_gif (SDL.RWops src);
[CCode (cname = "IMG_isJPG")]
public static bool is_jpg (SDL.RWops src);
[CCode (cname = "IMG_isTIF")]
public static bool is_tif (SDL.RWops src);
[CCode (cname = "IMG_isPNG")]
public static bool is_png (SDL.RWops src);
[CCode (cname = "IMG_isLBM")]
public static bool is_lbm (SDL.RWops src);
[CCode (cname = "IMG_isXV")]
public static bool is_xv (SDL.RWops src);
[CCode (cname = "IMG_isWEBP")]
public static bool is_webp (SDL.RWops src);
} // SDLImage
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2020 SDL2 VAPI Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Authors:
* Mario Daniel Ruiz Saavedra <desiderantes93@gmail.com>
* Gontzal Uriarte <txasatonga@gmail.com>
* Pedro H. Lara Campos <root@pedrohlc.com>
*/
[CCode (cheader_filename = "SDL2/SDL_ttf.h")]
namespace SDLTTF {
[CCode (cname = "TTF_Linked_Version")]
public static unowned SDL.Version linked ();
[CCode (cname = "TTF_ByteSwappedUNICODE")]
public static void byteswap_unicode (int swapped);
[CCode (cname = "TTF_Init")]
public static int init ();
[CCode (cname = "TTF_WasInit")]
public static bool is_initialized ();
[CCode (cname = "TTF_Quit")]
public static void quit ();
[CCode (cname = "int", cprefix = "TTF_STYLE_")]
public enum FontStyle {
NORMAL,
BOLD,
ITALIC,
STRIKETHROUGH,
UNDERLINE
}
[CCode (cname = "int", cprefix = "TTF_HINTING_")]
public enum FontHinting {
NORMAL,
LIGHT,
MONO,
NONE,
}
[CCode (cname = "TTF_Font", free_function = "TTF_CloseFont")]
[Compact]
public class Font {
[CCode (cname = "TTF_OpenFont")]
public Font (string file, int ptsize);
[CCode (cname = "TTF_OpenFontIndex")]
public Font.index (string file, int ptsize, long index);
[CCode (cname = "TTF_OpenFontRW")]
public Font.from_rwops (SDL.RWops src, int ptsize, [CCode (pos = "1.1")] bool freesrc = false);
[CCode (cname = "TTF_OpenFontIndexRW")]
public Font.from_rwops_index (SDL.RWops src, int ptsize, long index, [CCode (pos = "1.1")] bool freesrc = false);
[CCode (cname = "TTF_OpenFontDPIRW")]
public Font.from_rwops_dpi (SDL.RWops src, int ptsize, uint hdpi, uint vdpi, [CCode (pos = "1.1")] bool freesrc = false);
[CCode (cname = "TTF_OpenFontIndexDPIRW")]
public Font.from_rwops_index_dpi (SDL.RWops src, int ptsize, long index, uint hdpi, uint vdpi, [CCode (pos = "1.1")] bool freesrc = false);
[CCode (cname = "TTF_OpenFontDPI")]
public Font.dpi (string file, int ptsize, uint hdpi, uint vdpi);
[CCode (cname = "TTF_OpenFontIndexDPI")]
public Font.dpi_index (string file, int ptsize, long index, uint hdpi, uint vdpi);
public FontStyle font_style {
[CCode (cname = "TTF_GetFontStyle")]
get;
[CCode (cname = "TTF_SetFontStyle")]
set;