1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
| {$MODE OBJFPC}
{$M+}
unit Rendering;
interface
uses SDL2, Classes;
type
RenderingObject = class
private
function GetPosition:PSDL_Point;
function GetSize:PSDL_Point;
protected
_rectangle : PSDL_Rect;
public
procedure Update(elapsedTime : real); virtual;
procedure Draw(renderer : PSDL_Renderer); virtual;
procedure SetPosition(x, y: integer);
procedure SetSize(h, w : integer);
property Position : PSDL_Point read GetPosition;
property Size : PSDL_Point read GetSize;
constructor Initialize;
destructor Dispose;
end;
type
IContainer = interface
function GetPosition():PSDL_Rect;
procedure SetPosition(x, y : integer);
end;
type {Pointeurs sur types}
PIContainer = ^IContainer;
type
Surface = class(RenderingObject)
protected
_surface : PSDL_Surface;
_rotation : real;
_origin : PSDL_Point;
_worldOrigin : PSDL_Rect;
_flip : TSDL_RendererFlip;
_parent : PIContainer;
public
property Parent : PIContainer read _parent write _parent;
procedure LinkContainer(container : PIContainer);
constructor Initialize;
destructor Dispose;
end;
type
Camera2D = class(Surface)
end;
type
Texture = class(Surface)
protected
_texture : PSDL_Texture;
public
property Texture : PSDL_Texture read _texture write _texture;
constructor Initialize;
destructor Dispose;
end;
type
StaticTexture = class(Texture)
public
procedure Draw(renderer : PSDL_Renderer); override;
constructor Initialize;
destructor Dispose;
end;
type {Pointeurs sur types}
PRenderingObject = ^RenderingObject;
PSurface = ^Surface;
PStaticTexture = ^StaticTexture;
type
Group = class(TInterfacedObject, IContainer)
private
_children : TList;
_position : PSDL_Rect;
public
procedure Update(elapsedTime : real);
procedure Draw(renderer : PSDL_Renderer);
procedure AddChild(surface : PSurface);
procedure SetPosition(x, y : integer);
function GetPosition():PSDL_Rect;
constructor Initialize;
destructor Dispose;
end;
implementation
{Begin RenderingObject implementation}
constructor RenderingObject.Initialize;
begin
new(_rectangle);
end;
destructor RenderingObject.Dispose;
begin
end;
function RenderingObject.GetPosition:PSDL_Point;
begin
new(GetPosition);
GetPosition^.x := _rectangle^.x;
GetPosition^.y := _rectangle^.y;
end;
procedure RenderingObject.SetPosition(x, y: integer);
begin
_rectangle^.x := x;
_rectangle^.y := y;
end;
function RenderingObject.GetSize:PSDL_Point;
begin
new(GetSize);
GetSize^.x := _rectangle^.h;
GetSize^.y := _rectangle^.w;
end;
procedure RenderingObject.SetSize(h, w : integer);
begin
_rectangle^.h := h;
_rectangle^.w := w;
end;
procedure RenderingObject.Update(elapsedTime : real);
begin
end;
procedure RenderingObject.Draw(renderer : PSDL_Renderer);
begin
end;
{End RenderingObject implementation}
{Begin Group implementation}
constructor Group.Initialize;
begin
_children := TList.Create;
new(_position);
end;
destructor Group.Dispose;
begin
end;
procedure Group.Update(elapsedTime : real);
var
i : integer;
begin
for i := 0 to _children.count - 1 do
begin
PSurface(_children[i])^.Update(elapsedTime);
end;
end;
procedure Group.Draw(renderer : PSDL_Renderer);
var
i : integer;
begin
for i := 0 to _children.count - 1 do
begin
PSurface(_children[i])^.Draw(renderer);
end;
end;
procedure Group.AddChild(surface : PSurface);
begin
_children.Add(surface);
surface^.LinkContainer(PIContainer(self));
end;
procedure Group.SetPosition(x, y : integer);
begin
_position^.x := x;
_position^.y := y;
end;
function Group.GetPosition:PSDL_Rect;
begin
new(GetPosition);
GetPosition^.x := _position^.x;
GetPosition^.y := _position^.y;
end;
{End Group implementation}
{Begin Surface implementation}
constructor Surface.Initialize;
begin
inherited;
new(_origin);
_origin^.x := 0;
_origin^.y := 0;
new(_worldOrigin);
_worldOrigin^.x := 0;
_worldOrigin^.y := 0;
_rotation := 0;
_flip := SDL_FLIP_NONE;
end;
procedure Surface.LinkContainer(container : PIContainer);
begin
Parent := container;
end;
destructor Surface.Dispose;
begin
end;
{End Surface implementation}
{Begin Texture implementation}
constructor Texture.Initialize;
begin
inherited;
end;
destructor Texture.Dispose;
begin
end;
{End Texture implementation}
{Begin StaticTexture implementation}
constructor StaticTexture.Initialize;
begin
inherited;
end;
destructor StaticTexture.Dispose;
begin
end;
procedure StaticTexture.Draw(renderer : PSDL_Renderer);
begin
_worldOrigin^.x := _rectangle^.x;
_worldOrigin^.y := _rectangle^.y;
_worldOrigin^.w := _rectangle^.w;
_worldOrigin^.h := _rectangle^.h;
if Parent <> nil then
begin
_worldOrigin^.x := _worldOrigin^.x + Group(_parent).GetPosition()^.x;
_worldOrigin^.y := _worldOrigin^.y + Group(_parent).GetPosition()^.y;
end;
SDL_RenderCopyEx(renderer, _texture, nil, _worldOrigin, _rotation, _origin, _flip);
end;
{End StaticTexture implementation}
end. |
Partager