+ SDL renderer support

This commit is contained in:
Vovanium 2022-09-12 00:04:17 +03:00
parent 3d1bbd3c61
commit 0ea06131ce
4 changed files with 135 additions and 1 deletions

View File

@ -2,5 +2,5 @@ with "../video_sdl";
project Example_SDL extends "../common" is
for Object_Dir use "../build/objects";
for Exec_Dir use "../build/bin";
for Main use ("example_sdl_surface.adb");
for Main use ("example_sdl_render.adb", "example_sdl_surface.adb");
end Example_SDL;

View File

@ -0,0 +1,60 @@
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
with Interfaces.C;
with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Events.Events;
with Video.Colors;
with Video.Integer_Geometry;
with Video.SDL2.Renderers;
procedure Example_SDL_Render is
Screen_Width : constant := 640;
Screen_Height : constant := 480;
Wnd : aliased SDL.Video.Windows.Window;
Ev : SDL.Events.Events.Events;
use type SDL.Events.Event_Types;
begin
if not SDL.Initialise (Flags => SDL.Enable_Screen) then
raise Program_Error;
end if;
SDL.Video.Windows.Makers.Create (
Win => Wnd,
Title => "Hello World",
Position => SDL.Natural_Coordinates'(
SDL.Video.Windows.Centered_Window_Position,
SDL.Video.Windows.Centered_Window_Position),
Size => SDL.Positive_Sizes'(Screen_Width, Screen_Height),
Flags => 0);
declare
Rend : Video.SDL2.Renderers.SDL_Renderer;
begin
SDL.Video.Renderers.Makers.Create (Rend.Data, Wnd);
Rend.Fill_Rectangle (
Bounds => ((200, 399), (200, 299)),
Color => Video.Colors.RGB_8 (16#FA#, 16#CE#, 16#8D#));
for Y in 0 .. 255 loop
for X in 0 .. 255 loop
Rend.Write_Pixel (
Location => (X, Y),
Color => Video.Colors.RGB_8 (X, Y, 255 - (X + Y) / 2));
end loop;
end loop;
end;
Wait : loop
while SDL.Events.Events.Poll (Ev) loop
exit Wait when Ev.Common.Event_Type = SDL.Events.Quit;
end loop;
end loop Wait;
Wnd.Finalize;
SDL.Finalise;
end Example_SDL_Render;

View File

@ -0,0 +1,44 @@
with SDL.Video.Rectangles;
with Video.SDL2.Rectangles;
with Video.SDL2.Colors;
package body Video.SDL2.Renderers is
function Bounding_Box (R : SDL_Renderer) return Integer_Geometry.Box is
Rect : SDL.Video.Rectangles.Rectangle;
begin
R.Data.Get_Viewport (Rect);
return Video.SDL2.Rectangles.From_SDL (Rect);
end Bounding_Box;
procedure Write_Pixel (
Target : in out SDL_Renderer;
Location : Integer_Geometry.Point;
Color : Video.Colors.Color)
is
begin
Target.Data.Set_Draw_Colour (Video.SDL2.Colors.To_SDL (Color));
Target.Data.Draw (Rectangles.To_SDL (Location));
end Write_Pixel;
procedure Fill_Rectangle (
Target : in out SDL_Renderer;
Bounds : Integer_Geometry.Box;
Color : Video.Colors.Color)
is
begin
Target.Data.Set_Draw_Colour (Video.SDL2.Colors.To_SDL (Color));
Target.Data.Fill (Rectangles.To_SDL (Bounds));
end Fill_Rectangle;
procedure Finalize (Target : in out SDL_Renderer) is
begin
Target.Sync;
end;
procedure Sync (Target : in out SDL_Renderer) is
begin
Target.Data.Present;
end;
end Video.SDL2.Renderers;

View File

@ -0,0 +1,30 @@
with Ada.Finalization;
with SDL.Video.Renderers;
with Video.Colors;
with Video.Integer_Geometry;
with Video.Renderables.Color;
package Video.SDL2.Renderers is
type SDL_Renderer is limited new Ada.Finalization.Limited_Controlled
and Video.Renderables.Color.Color_Renderable with record
Data : SDL.Video.Renderers.Renderer;
end record;
overriding function Bounding_Box (R : SDL_Renderer) return Integer_Geometry.Box;
overriding procedure Write_Pixel (
Target : in out SDL_Renderer;
Location : Integer_Geometry.Point;
Color : Video.Colors.Color);
overriding procedure Fill_Rectangle (
Target : in out SDL_Renderer;
Bounds : Integer_Geometry.Box;
Color : Video.Colors.Color);
overriding procedure Finalize (Target : in out SDL_Renderer);
overriding procedure Sync (Target : in out SDL_Renderer);
end Video.SDL2.Renderers;