aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--graphics.adb39
-rw-r--r--graphics.ads31
-rw-r--r--queue.adb8
4 files changed, 51 insertions, 29 deletions
diff --git a/README.md b/README.md
index 18b3a28..1d2dde1 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Snake
-Snake Game in Ada, This is SDL version of a TP on [openclassrooms](https://openclassrooms.com/fr/courses/900279-apprenez-a-programmer-avec-ada/2144821-tp-le-jeu-du-serpent).
+Snake Game in Ada, This is an SDL version of [this](https://openclassrooms.com/fr/courses/900279-apprenez-a-programmer-avec-ada/2144821-tp-le-jeu-du-serpent) on Openclassrooms.
# Usage
diff --git a/graphics.adb b/graphics.adb
index 2cdea69..e9e4b71 100644
--- a/graphics.adb
+++ b/graphics.adb
@@ -1,29 +1,37 @@
with Ada.Text_IO;
use Ada.Text_IO;
-with Graphics;
-use Graphics;
-
with SDL;
with SDL.Timers;
+with SDL.Video.Rectangles;
with SDL.Events;
with SDL.Events.Events;
with SDL.Video.Windows;
with SDL.Video.Windows.Makers;
with SDL.Video.Renderers;
with SDL.Video.Renderers.Makers;
+use SDL.Video;
+
+with Graphics, Game;
+use Graphics, Game;
+
+with Interfaces.C;
+use Interfaces.C;
package body Graphics is
+ use P_Position_Queue;
+
procedure Init(state: out T_State) is
begin
if not SDL.Initialise then
Put("Error");
end if;
- SDL.Video.Windows.Makers.Create(state.window, WINDOW_TITLE, WINDOW_X, WINDOW_Y,
+ Windows.Makers.Create(state.window, WINDOW_TITLE, WINDOW_X, WINDOW_Y,
WINDOW_WIDTH, WINDOW_HEIGHT);
- SDL.Video.Renderers.Makers.Create(state.renderer, state.window);
+ Renderers.Makers.Create(state.renderer, state.window);
state.running := true;
+ Init(state.game, 10, 10);
end Init;
procedure Quit(state: T_State) is
@@ -52,9 +60,26 @@ package body Graphics is
null;
end Event_Handler;
- procedure Update(state: T_State) is
+ procedure Update(state: in out T_State) is
+ cursor: T_List := state.game.snake.front;
begin
- null;
+ Renderers.Set_Draw_Colour(state.renderer, COLOR_BLACK);
+ Renderers.Clear(state.renderer);
+
+ Renderers.Set_Draw_Colour(state.renderer, COLOR_WHITE);
+ while cursor /= null loop
+ Draw_Square(state, cursor.all.data);
+ cursor := cursor.all.next;
+ end loop;
+ Renderers.Present(state.renderer);
end Update;
+ procedure Draw_Square(state: in out T_State;
+ pos: T_Position) is
+ rect: Rectangles.Rectangle;
+ begin
+ rect := ((C.int(pos.y) - 1) * 20, (C.int(pos.x) - 1) * 20, 20, 20);
+ Renderers.Fill(state.renderer, rect);
+ end Draw_Square;
+
end Graphics;
diff --git a/graphics.ads b/graphics.ads
index 1450381..1f58de3 100644
--- a/graphics.ads
+++ b/graphics.ads
@@ -1,19 +1,10 @@
with SDL;
-
--- with Game;
--- use Game;
-
-with SDL.Video.Windows.Makers;
-use SDL.Video.Windows.Makers;
-
-with SDL.Video.Renderers.Makers;
-use SDL.Video.Renderers.Makers;
-
+with SDL.Video.Windows;
with SDL.Video.Renderers;
-use SDL.Video.Renderers;
+with SDL.Video.Palettes;
-with SDL.Video.Windows;
-use SDL.Video.Windows;
+with Game;
+use Game;
package Graphics is
@@ -23,9 +14,14 @@ package Graphics is
WINDOW_WIDTH: constant SDL.Positive_Dimension := 400;
WINDOW_HEIGHT: constant SDL.Positive_Dimension := 400;
+ COLOR_WHITE: constant SDL.Video.Palettes.Colour := (255, 255, 255, 255);
+ COLOR_BLACK: constant SDL.Video.Palettes.Colour := (0, 0, 0, 255);
+ COLOR_RED: constant SDL.Video.Palettes.Colour := (255, 0, 0, 255);
+ COLOR_GREEN: constant SDL.Video.Palettes.Colour := (0, 255, 0, 255);
+
type T_State is record
running: Boolean;
- -- game: T_Game;
+ game: T_Game;
window: SDL.Video.Windows.Window;
renderer: SDL.Video.Renderers.Renderer;
end record;
@@ -33,6 +29,11 @@ package Graphics is
procedure Init(state: out T_State);
procedure Quit(state: T_State);
procedure Run(state: in out T_State);
+
+private
+
procedure Event_Handler(state: in out T_State);
- procedure Update(state: T_State);
+ procedure Update(state: in out T_State);
+ procedure Draw_Square(state: in out T_State;
+ pos: T_Position);
end Graphics;
diff --git a/queue.adb b/queue.adb
index 2869984..1bafa1d 100644
--- a/queue.adb
+++ b/queue.adb
@@ -1,19 +1,15 @@
package body Queue is
procedure Enqueue(queue: in out T_Queue; data: T_Data) is
- element: T_List_Node;
new_back: T_List;
begin
- element.data := data;
- element.next := null;
- new_back := new T_List_Node'(element);
-
+ new_back := new T_List_Node'((data => data,
+ next => null));
if Empty(queue) then
queue.front := new_back;
queue.back := new_back;
return;
end if;
-
queue.back.next := new_back;
queue.back := new_back;
end Enqueue;