Commit e02e3fad 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',
'fonts/antar.ttf',
],
install_dir: join_paths(get_option('datadir'), meson.project_name(), 'fonts')
)
# Install images
install_data(
[
'gfx/1024x1024GreenNebula4.png',
],
install_dir: join_paths(get_option('datadir'), meson.project_name(), 'gfx')
)
project('demofonts2', ['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')
gfxdir = join_paths(datadir, 'gfx')
conf = configuration_data ()
conf.set_quoted ('VERSION', meson.project_version())
conf.set_quoted ('GETTEXT_PACKAGE', meson.project_name())
conf.set_quoted ('GFX_DIRECTORY', gfxdir)
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')
public class GSDL.GameLoop : Object {
private GSDL.Wsdl wsdl;
private GSDL.Input input;
private RBackGround rb_space;
private SDLTTF.Font jetmono20;
private SDLTTF.Font antar90;
private RFont current_fps;
private RFont msg_solid;
private RFont msg_shaded;
private RFont msg_blended;
public GameLoop () {
wsdl = new GSDL.Wsdl ();
input = new GSDL.Input ();
load_resources ();
create_messages ();
}
~GameLoop () {
}
private void load_resources () {
jetmono20 = RFont.load_font_file ("JetMono-EB.ttf");
antar90 = RFont.load_font_file ("antar.ttf", 90);
rb_space = new RBackGround (wsdl, "1024x1024GreenNebula4.png");
}
private void create_messages () {
current_fps = new RFont (wsdl, jetmono20);
msg_solid = new RFont (wsdl, antar90);
msg_solid.color = {255, 255, 255, 255};
msg_shaded = new RFont (wsdl, antar90);
msg_shaded.color = {255, 255, 255, 255};
msg_blended = new RFont (wsdl, antar90);
msg_blended.color = {255, 255, 255, 255};
}
public void run () {
uint32 previous = wsdl.get_time ();
double lag = 0.0f;
while (!input.done) {
wsdl.start_performance_counter ();
uint32 current = wsdl.get_time ();
uint32 elapsed = current - previous;
previous = current;
lag += elapsed;
wsdl.prepare_scene ();
input.process_events ();
while (lag >= Wsdl.MS_PER_UPDATE) {
update ();
lag -= Wsdl.MS_PER_UPDATE;
}
draw ();
wsdl.present_scene ();
wsdl.wait (1);
wsdl.end_performance_counter ();
}
}
private void update () {
rb_space.update ();
msg_solid.set_text_solid ("Render Solid");
msg_shaded.set_text_shaded ("Render Shaded");
msg_blended.set_text_blended ("Render Blended", 680);
current_fps.set_text_blended (wsdl.get_FPS_string (), 200);
}
private void draw () {
rb_space.draw ();
msg_solid.write (220, 120);
msg_shaded.write (200, 320);
msg_blended.write (180, 520);
current_fps.write (10, wsdl.height () - 30);
}
}
/*
* DemoGameLoop.vala
*
* (c) Author: David Quiroga
* e-mail: david [at] clibre [dot] io
*
****************************************************************
* Description:
*
* Game Loop in SDL2 with Vala
*
* SPDX-License-Identifier: GPL-3.0
*/
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/input.vala',
'sdl/rtexture.vala',
'sdl/rbackground.vala',
'sdl/rfont.vala',
'gameloop.vala',
]
app_deps = [
dependency('glib-2.0'),
dependency('gobject-2.0'),
dependency('gio-2.0'),
dependency('sdl2'),
dependency('SDL2_image'),
dependency('SDL2_gfx'),
dependency('SDL2_ttf'),
]
add_project_arguments([
'--vapidir', join_paths(meson.current_source_dir(), 'vapi'),
'--target-glib=2.50',
],
language: 'vala'
)
executable('demofonts2', app_sources, dependencies: app_deps,
install: true,
c_args: [
'-include', 'config.h'
]
)
using SDL;
using SDL;
public class GSDL.Input
{
public bool done { get; private set; default = false; }
public Input () {
}
public void process_events () {
Event event;
while (Event.poll (out event) != 0) {
switch (event.type) {
case EventType.QUIT:
this._done = true;
break;
case EventType.MOUSEBUTTONDOWN:
this._done = true;
break;
default:
break;
}
}
}
}
using SDL;
using SDL.Video;
using SDLImage;
public class GSDL.RBackGround : GSDL.RTexture
{
private int _backgroundX = 0;
public RBackGround (Wsdl wsdl, string texture_file) {
base (wsdl, texture_file, 1);
}
public void draw () {
var dest = Video.Rect();
var scrw = base.wsdl.width ();
var scrh = base.wsdl.height ();
for (int x = this._backgroundX; x < scrw; x += scrw) {
dest.x = x;
dest.y = 0;
dest.w = scrw;
dest.h = scrh;
base.wsdl.renderer.copy (this.texture, null, dest);
}
}
public void update () {
if (--this._backgroundX < -base.wsdl.width ())
this._backgroundX = 0;
}
}
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 });
}
}
using SDL;
using SDL.Video;
using SDLImage;
public class GSDL.RTexture
{
protected unowned Wsdl wsdl;
protected Texture? texture;
public Rect rect { get; private set; }
private int total_frames = 1;
public RTexture (Wsdl wsdl, string texture_file, int total_frames) {
this.wsdl = wsdl;
load_texture_file (texture_file);
this.total_frames = total_frames;
this._rect = Video.Rect ();
this._rect.x = 0;
this._rect.y = 0;
this.texture.query (null, null, out this._rect.w, out this._rect.h);
this._rect.w /= total_frames;
}
private void load_texture_file (string file) {
string path = Path.build_filename (GFX_DIRECTORY, file);
check_resource (path);
this.texture = load_texture (wsdl.renderer, path);
assert (this.texture != null);
}
private 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 copy (int position_x, int position_y, int delay_per_frame) {
var dest = Video.Rect();
int frame = (int) ( SDL.Timer.get_ticks () / delay_per_frame) % total_frames;
this._rect.x = (int) (frame * this._rect.w);
dest.x = position_x;
dest.y = position_y;
dest.w = this._rect.w;
dest.h = this._rect.h;
wsdl.renderer.copy (texture, this._rect, dest);
}
}
using SDL;
using SDLGraphics;
public class GSDL.Wsdl : Object {
private const string window_name = "Fonts SDL Demo2";
private const int DEFAULT_SCREEN_WIDTH = 1024;
private const int DEFAULT_SCREEN_HEIGHT = 768;
private static int w_width = DEFAULT_SCREEN_WIDTH;
private static int w_height = DEFAULT_SCREEN_HEIGHT;
public const double MS_PER_UPDATE = 20;
private Video.Window window;
public Video.Renderer? renderer;
private uint64 start_perf_counter = 0;
private uint64 end_perf_counter = 0;
private float elap = 0;
public Wsdl () {
if (SDL.init (SDL.InitFlag.VIDEO) != 0) {
critical ("Couldn't initialize SDL: %s\n", SDL.get_error());
}
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);
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);
}
~Wsdl () {
SDL.quit ();
}
public int width () {
return w_width;
}
public int height () {
return w_height;
}
public void prepare_scene () {
renderer.set_draw_color (32, 32, 32, 255);
renderer.clear();
}
public void present_scene () {
renderer.present ();
}
public uint32 get_time () {
return SDL.Timer.get_ticks ();
}
public void wait (uint32 ms) {
SDL.Timer.delay (ms);
}
public void start_performance_counter () {
start_perf_counter = SDL.Timer.get_performance_counter ();
}
public void end_performance_counter () {
end_perf_counter = SDL.Timer.get_performance_counter ();
elap = (end_perf_counter - start_perf_counter) /
(float) SDL.Timer.get_performance_frequency ();
}
public float get_FPS () {
return (1.0f / elap);
}
public string get_FPS_string () {
string current_fps = "Curren FPS: %0.0f".printf (get_FPS ());
return current_fps;
}
}
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);