AdaCore / Ada-SPARK-Crate-Of-The-Year

19 stars 2 forks source link

[2022][sdlada] Ada 2012 (variable depth) bindings to SDL 2.x for game (and application) development. #22

Closed Lucretia closed 1 year ago

Lucretia commented 1 year ago

sdlada

sdlada

Home

SDLAda

Purpose

SDL provides cross-platform low-level OS primitives to handle windowing, events, basic 2D/3D drawing and other media capabilities mainly for the development of games and has been used in many AA/AAA games in the past 25 years. SDL 2.x, released in 2013, is a non backwards compatible (with SDL 1.x) library for more modern systems where 3D capabilities are the default.

SDLAda provides a type safe Ada 2012 language binding for SDL 2.x, enabling the developer to build a portable game or other application in less time and with less errors due to the inherent type safety of the language.

Having worked in the games industry using C and C++, I know just how badly things can go when there are no checks on the code, this library is my response to that.

Usage

Develop portable 2D/3D games across multiple OSes and devices (toolchain allowing) with minimal changes or applications which don't require a larger GUI toolkit such as Gtk+/Qt.

A simple hello, world style application which opens a resizable window and awaits the user to click the close button, would look like this:

with SDL;
with SDL.Events.Events;
with SDL.Log;
with SDL.Video.Pixel_Formats;
with SDL.Video.Renderers.Makers;
with SDL.Video.Textures.Makers;
with SDL.Video.Windows.Makers;

procedure Hello_World is
   W                : SDL.Video.Windows.Window;
   W_Size           : constant SDL.Positive_Sizes := (800, 640);
   Renderer         : SDL.Video.Renderers.Renderer;
   Texture          : SDL.Video.Textures.Texture;
   Event            : SDL.Events.Events.Events;

   use type SDL.Events.Event_Types;
begin
   SDL.Log.Set (Category => SDL.Log.Application, Priority => SDL.Log.Debug);

   if SDL.Initialise = True then
      SDL.Video.Windows.Makers.Create
        (Win      => W,
         Title    => "Hello, World",
         Position => SDL.Natural_Coordinates'(X => 100, Y => 100),
         Size     => W_Size,
         Flags    => SDL.Video.Windows.Resizable);

      SDL.Video.Renderers.Makers.Create (Renderer, W);

      --  Set the texture to the same size as the window, as the window scales, the texture
      --  will also scale, it will *not* be rebuilt to match the new window size.
      SDL.Video.Textures.Makers.Create
        (Tex      => Texture,
         Renderer => Renderer,
         Format   => SDL.Video.Pixel_Formats.Pixel_Format_ARGB_8888,
         Kind     => SDL.Video.Textures.Streaming,
         Size     => W_Size);

      Main : loop
         while SDL.Events.Events.Poll (Event) loop
            if Event.Common.Event_Type = SDL.Events.Quit then
               exit Main;
            end if;

            Renderer.Clear;
            Renderer.Copy (Texture);
            Renderer.Present;
         end loop;
      end loop Main;

      -- Clean up and exit.
      W.Finalize;
      SDL.Finalise;
   end if;
end Hello_World;

I will include this in the repo as a basic test soon.

Repo example

stream2

Third party examples

Jesper Quorning's Olofson and ballfield

paralax ballfield anim

Design

The aim of the design was to create as much of an Ada feeling API as possible without hitting performance by doing too much/if any copying/translation of data between language layers.

This is a multi-depth binding in which some functionality is thinly bound where there are C void functions and others are bound with a thicker binding hiding the original C code underneath, types are mostly mapped directly from Ada with C convention to reduce the need for mapping between language types.

There are higher level abstractions, in progress, for surfaces and textures, which will utilise a generic to wrap the C pointer of 1/2/3D image data.

Authors

License

Zlib