ada_voxel_space_demo_1.0.1_69740326/src/main.adb

  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
with Interfaces;    use Interfaces;
with Ada.Real_Time; use Ada.Real_Time;

with Ada_Voxel;
with SDL_Display;
with Color_Map;
with Height_Map;
with Keyboard;

with Ada.Numerics.Generic_Elementary_Functions;

procedure Main is

   Screen_Width  : constant := 800;
   Screen_Height : constant := 600;

   package Float_Functions is
     new Ada.Numerics.Generic_Elementary_Functions (Float);

   package Display is new SDL_Display (Screen_Width, Screen_Height);

   function Color_Map (X, Y : Integer) return Display.SDL_Pixel;

   function Height_Map (X, Y : Integer) return Integer;

   package Voxel is new Ada_Voxel
     (Color              => Display.SDL_Pixel,
      Screen_Width       => Screen_Width,
      Screen_Height      => Screen_Height,
      Color_Map          => Color_Map,
      Height_Map         => Height_Map,
      Draw_Vertical_Line => Display.Draw_Vertical_Line);

   ---------------
   -- Color_Map --
   ---------------

   function Color_Map (X, Y : Integer) return Display.SDL_Pixel is
      C   : constant Unsigned_8 :=
        Standard.Color_Map.Map ((Integer (X) mod 1024) + 1024 * (Integer (Y) mod 1024));
      RGB : constant Standard.Color_Map.RGB := Standard.Color_Map.Palette (C);
   begin
      return Display.To_SDL_Color (RGB.R, RGB.G, RGB.B);
   end Color_Map;

   ----------------
   -- Height_Map --
   ----------------

   function Height_Map (X, Y : Integer) return Integer is
   begin
      return Integer (Standard.Height_Map.Map ((Integer (X) mod 1024) + 1024 * (Integer (Y) mod 1024)));
   end Height_Map;

   Cam_X      : Float := 1060.0;
   Cam_Y      : Float := -350.0;
   Cam_Angle  : Float := 5.4;
   Cam_Height : Float := 120.0;

   Period       : constant Time_Span := To_Time_Span (1.0 / 60.0);
   Next_Release : Time := Clock + Period;

begin
   loop
      Keyboard.Update;
      if Keyboard.Pressed (Keyboard.Up) then
         Cam_Height := Cam_Height + 0.75;
      end if;
      if Keyboard.Pressed (Keyboard.Down) then
         Cam_Height := Cam_Height - 0.75;
      end if;
      if Keyboard.Pressed (Keyboard.Left) then
         Cam_Angle := Cam_Angle + 0.03;
      end if;
      if Keyboard.Pressed (Keyboard.Right) then
         Cam_Angle := Cam_Angle - 0.03;
      end if;
      if Keyboard.Pressed (Keyboard.Forward) then
         Cam_X := Cam_X - Float_Functions.Sin (Cam_Angle);
         Cam_Y := Cam_Y - Float_Functions.Cos (Cam_Angle);
      end if;
      if Keyboard.Pressed (Keyboard.Backward) then
         Cam_X := Cam_X + Float_Functions.Sin (Cam_Angle);
         Cam_Y := Cam_Y + Float_Functions.Cos (Cam_Angle);
      end if;
      if Keyboard.Pressed (Keyboard.Esc) then
         return;
      end if;

      if Cam_Height < Float (Height_Map (Integer (Cam_X), Integer (Cam_Y))) + 15.0 then
         Cam_Height := Float (Height_Map (Integer (Cam_X), Integer (Cam_Y))) + 15.0;
      end if;

      Display.Start_Render;

      Display.Fill (Display.To_SDL_Color (135, 206, 250));

      Voxel.Render (Cam_X,
                    Cam_Y,
                    Cam_Angle,
                    Cam_Height,
                    Horizon      => 60.0,
                    Distance     => 400.0,
                    Scale_Height => 200.0);
      Display.End_Render;

--        delay until Next_Release;
--        Next_Release := Next_Release + Period;
   end loop;
end Main;