From 1bf3c1a62f90ce02db27f13e8d66ec80a3b264d3 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Mon, 3 Aug 2020 22:02:05 -0700 Subject: [PATCH] Setting up open gl stuff --- SampleGame/BeefProj.toml | 4 ++ SampleGame/BeefSpace.toml | 6 +++ SampleGame/src/Program.bf | 3 +- src/Assets/Sprite.bf | 2 +- src/Core/Game.bf | 1 + src/PlatformLayer/PlatformLayer.bf | 13 +++---- src/PlatformLayer/SDL2PlatformLayer.bf | 40 +++++++++++++------- src/Static/Draw.bf | 52 +++++++++++++------------- 8 files changed, 73 insertions(+), 48 deletions(-) diff --git a/SampleGame/BeefProj.toml b/SampleGame/BeefProj.toml index 3d73154..c003121 100644 --- a/SampleGame/BeefProj.toml +++ b/SampleGame/BeefProj.toml @@ -6,3 +6,7 @@ Name = "SampleGame" TargetType = "BeefGUIApplication" StartupObject = "Strawberry.Sample.Program" DefaultNamespace = "Strawberry.Sample" + +[[Project.DistinctOptions]] +Filter = "" +ReflectAlwaysInclude = "IncludeAll" diff --git a/SampleGame/BeefSpace.toml b/SampleGame/BeefSpace.toml index aa4639d..8abb2a6 100644 --- a/SampleGame/BeefSpace.toml +++ b/SampleGame/BeefSpace.toml @@ -3,3 +3,9 @@ Projects = {SampleGame = {Path = "."}, Strawberry = {Path = ".."}, SDL2 = "*"} [Workspace] StartupProject = "SampleGame" + +[[Workspace.DistinctOptions]] +Filter = "" + +[Configs.Debug.Win64] +Toolset = "LLVM" diff --git a/SampleGame/src/Program.bf b/SampleGame/src/Program.bf index 76d557b..d98ad92 100644 --- a/SampleGame/src/Program.bf +++ b/SampleGame/src/Program.bf @@ -6,8 +6,9 @@ namespace Strawberry.Sample { static public int Main(String[] args) { - SDL2PlatformLayer sdl = scope SDL2PlatformLayer(); + let sdl = scope SDL2PlatformLayer(); let game = scope SampleGame(sdl); + game.Run(); return 0; } diff --git a/src/Assets/Sprite.bf b/src/Assets/Sprite.bf index 720d243..e2b61b0 100644 --- a/src/Assets/Sprite.bf +++ b/src/Assets/Sprite.bf @@ -437,7 +437,7 @@ namespace Strawberry public this(int w, int h) { - Texture = SDL.CreateTexture(Game.Renderer, (uint32)SDL.PIXELFORMAT_RGBA8888, (int32)SDL.TextureAccess.Streaming, (int32)w, (int32)h); + //Texture = SDL.CreateTexture(Game.Renderer, (uint32)SDL.PIXELFORMAT_RGBA8888, (int32)SDL.TextureAccess.Streaming, (int32)w, (int32)h); Width = w; Height = h; diff --git a/src/Core/Game.bf b/src/Core/Game.bf index 266dad6..d2b1ac1 100644 --- a/src/Core/Game.bf +++ b/src/Core/Game.bf @@ -38,6 +38,7 @@ namespace Strawberry : base() { Game = this; + PlatformLayer = platformLayer; Title = windowTitle; Width = width; diff --git a/src/PlatformLayer/PlatformLayer.bf b/src/PlatformLayer/PlatformLayer.bf index 7828199..9acf134 100644 --- a/src/PlatformLayer/PlatformLayer.bf +++ b/src/PlatformLayer/PlatformLayer.bf @@ -4,20 +4,19 @@ namespace Strawberry public abstract class PlatformLayer { public abstract void Init(); + public abstract uint32 Tick(); // Returns milliseconds since last tick + public abstract bool Closed(); // If the game window has been closed + + //Rendering public abstract void RenderBegin(); public abstract void RenderEnd(); - public abstract void UpdateInput(); + //Input + public abstract void UpdateInput(); public abstract bool PollKey(Keys key); public abstract bool CapsLock { get; } public abstract bool NumLock { get; } public abstract bool PollGamepadButton(int gamepadID, Buttons button); public abstract float PollGamepadAxis(int gamepadID, Axes axis); - - // Returns milliseconds since last tick - public abstract uint32 Tick(); - - // If the game window has been closed - public abstract bool Closed(); } } diff --git a/src/PlatformLayer/SDL2PlatformLayer.bf b/src/PlatformLayer/SDL2PlatformLayer.bf index 4a896dc..ae24d5f 100644 --- a/src/PlatformLayer/SDL2PlatformLayer.bf +++ b/src/PlatformLayer/SDL2PlatformLayer.bf @@ -6,10 +6,11 @@ namespace Strawberry { public class SDL2PlatformLayer : PlatformLayer { - private SDL.Rect screenRect; private SDL.Window* window; private SDL.Surface* screen; + private SDL.Rect screenRect; private SDL.Renderer* renderer; + private SDL.SDL_GLContext glContext; private SDL.SDL_GameController*[] gamepads; private bool* keyboard; @@ -35,19 +36,32 @@ namespace Strawberry SDL.EventState(.JoyButtonUp, .Disable); SDL.EventState(.JoyDeviceAdded, .Disable); SDL.EventState(.JoyDeviceRemoved, .Disable); - - window = SDL.CreateWindow(Game.Title, .Centered, .Centered, screenRect.w, screenRect.h, .Shown); - screenRect = SDL.Rect(0, 0, (int32)(Game.Width * Game.WindowScale), (int32)(Game.Height * Game.WindowScale)); - renderer = SDL.CreateRenderer(window, -1, .Accelerated); - screen = SDL.GetWindowSurface(window); - SDLImage.Init(.PNG | .JPG); - SDLMixer.OpenAudio(44100, SDLMixer.MIX_DEFAULT_FORMAT, 2, 4096); - SDLTTF.Init(); - keyboard = SDL.GetKeyboardState(null); - gamepads = new SDL.SDL_GameController*[Game.GamepadLimit]; - for (let i < gamepads.Count) - gamepads[i] = SDL.GameControllerOpen((int32)i); + //Graphics + { + window = SDL.CreateWindow(Game.Title, .Centered, .Centered, screenRect.w, screenRect.h, .Shown); + screenRect = SDL.Rect(0, 0, (int32)(Game.Width * Game.WindowScale), (int32)(Game.Height * Game.WindowScale)); + renderer = SDL.CreateRenderer(window, -1, .Accelerated); + screen = SDL.GetWindowSurface(window); + SDLImage.Init(.PNG | .JPG); + SDLTTF.Init(); + + glContext = SDL.GL_CreateContext(window); + SDL.GL_SetSwapInterval(1); + } + + //Audio + { + SDLMixer.OpenAudio(44100, SDLMixer.MIX_DEFAULT_FORMAT, 2, 4096); + } + + //Input + { + keyboard = SDL.GetKeyboardState(null); + gamepads = new SDL.SDL_GameController*[Game.GamepadLimit]; + for (let i < gamepads.Count) + gamepads[i] = SDL.GameControllerOpen((int32)i); + } } public ~this() diff --git a/src/Static/Draw.bf b/src/Static/Draw.bf index 51c5cb8..f967df6 100644 --- a/src/Static/Draw.bf +++ b/src/Static/Draw.bf @@ -27,9 +27,9 @@ namespace Strawberry static public void Rect(int x, int y, int w, int h, Color color) { - SDL.SetRenderDrawBlendMode(Game.Renderer, .Blend); - SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); - SDL.RenderFillRect(Game.Renderer, &SDL.Rect((int32)(x - Camera.X), (int32)(y - Camera.Y), (int32)w, (int32)h)); + //SDL.SetRenderDrawBlendMode(Game.Renderer, .Blend); + //SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + //SDL.RenderFillRect(Game.Renderer, &SDL.Rect((int32)(x - Camera.X), (int32)(y - Camera.Y), (int32)w, (int32)h)); } static public void Rect(Rect rect, Color color) @@ -39,9 +39,9 @@ namespace Strawberry static public void HollowRect(int x, int y, int w, int h, Color color) { - SDL.SetRenderDrawBlendMode(Game.Renderer, .Blend); - SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); - SDL.RenderDrawRect(Game.Renderer, &SDL.Rect((int32)(x - Camera.X), (int32)(y - Camera.Y), (int32)w, (int32)h)); + //SDL.SetRenderDrawBlendMode(Game.Renderer, .Blend); + //SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + //SDL.RenderDrawRect(Game.Renderer, &SDL.Rect((int32)(x - Camera.X), (int32)(y - Camera.Y), (int32)w, (int32)h)); } static public void HollowRect(Rect rect, Color color) @@ -54,9 +54,9 @@ namespace Strawberry let fromn = (Point)(from - Camera); let ton = (Point)(to - Camera); - SDL.SetRenderDrawBlendMode(Game.Renderer, .Blend); - SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); - SDL.RenderDrawLine(Game.Renderer, (int32)fromn.X, (int32)fromn.Y, (int32)ton.X, (int32)ton.Y); + //SDL.SetRenderDrawBlendMode(Game.Renderer, .Blend); + //SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + //SDL.RenderDrawLine(Game.Renderer, (int32)fromn.X, (int32)fromn.Y, (int32)ton.X, (int32)ton.Y); } static public void Circle(Point at, float radius, Color color, int steps = 16) @@ -76,9 +76,9 @@ namespace Strawberry ); } - SDL.SetRenderDrawBlendMode(Game.Renderer, .Blend); - SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); - SDL.RenderDrawLines(Game.Renderer, &points[0], (int32)steps + 1); + //SDL.SetRenderDrawBlendMode(Game.Renderer, .Blend); + //SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + //SDL.RenderDrawLines(Game.Renderer, &points[0], (int32)steps + 1); } static public void Sprite(Sprite sprite, int frame, Point position) @@ -86,8 +86,8 @@ namespace Strawberry SDL.Rect src = Strawberry.Rect(0, 0, sprite.Width, sprite.Height); SDL.Rect dst = Strawberry.Rect(position.X - Camera.X, position.Y - Camera.Y, sprite.Width, sprite.Height); - SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); - SDL.RenderCopy(Game.Renderer, sprite[frame].Texture, &src, &dst); + //SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); + //SDL.RenderCopy(Game.Renderer, sprite[frame].Texture, &src, &dst); } static public void Sprite(Sprite sprite, int frame, Point position, Point origin) @@ -96,8 +96,8 @@ namespace Strawberry SDL.Rect src = Strawberry.Rect(0, 0, sprite.Width, sprite.Height); SDL.Rect dst = Strawberry.Rect(position.X - origin.X - Camera.X, position.Y - origin.Y - Camera.Y, sprite.Width, sprite.Height); - SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); - SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, 0, &cnt, .None); + //SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); + //SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, 0, &cnt, .None); } static public void Sprite(Sprite sprite, int frame, Point position, Point origin, float rotation) @@ -106,8 +106,8 @@ namespace Strawberry SDL.Rect src = Strawberry.Rect(0, 0, sprite.Width, sprite.Height); SDL.Rect dst = Strawberry.Rect(position.X - origin.X - Camera.X, position.Y - origin.Y - Camera.Y, sprite.Width, sprite.Height); - SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); - SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, rotation, &cnt, .None); + //SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); + //SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, rotation, &cnt, .None); } static public void SpriteCentered(Sprite sprite, int frame, Point position) @@ -117,8 +117,8 @@ namespace Strawberry SDL.Rect src = Strawberry.Rect(0, 0, sprite.Width, sprite.Height); SDL.Rect dst = Strawberry.Rect(position.X - origin.X - Camera.X, position.Y - origin.Y - Camera.Y, sprite.Width, sprite.Height); - SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); - SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, 0, &cnt, .None); + //SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); + //SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, 0, &cnt, .None); } static public void SpriteCentered(Sprite sprite, int frame, Point position, Color color) @@ -131,7 +131,7 @@ namespace Strawberry SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); SDL.SetTextureColorMod(sprite[frame].Texture, color.R, color.G, color.B); SDL.SetTextureAlphaMod(sprite[frame].Texture, color.A); - SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, 0, &cnt, .None); + //SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, 0, &cnt, .None); } static public void SpriteCentered(Sprite sprite, int frame, Point position, float rotation) @@ -142,20 +142,20 @@ namespace Strawberry SDL.Rect dst = Strawberry.Rect(position.X - origin.X - Camera.X, position.Y - origin.Y - Camera.Y, sprite.Width, sprite.Height); SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); - SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, rotation, &cnt, .None); + //SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, rotation, &cnt, .None); } static public void Text(SDL2.SDLTTF.Font* font, String text, Point position, Color color) { - SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + //SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); let surface = SDLTTF.RenderUTF8_Solid(font, text, color); - let texture = SDL.CreateTextureFromSurface(Game.Renderer, surface); + //let texture = SDL.CreateTextureFromSurface(Game.Renderer, surface); SDL.Rect srcRect = .(0, 0, surface.w, surface.h); SDL.Rect destRect = .((int32)position.X, (int32)position.Y, surface.w, surface.h); - SDL.RenderCopy(Game.Renderer, texture, &srcRect, &destRect); + //SDL.RenderCopy(Game.Renderer, texture, &srcRect, &destRect); SDL.FreeSurface(surface); - SDL.DestroyTexture(texture); + //SDL.DestroyTexture(texture); } } }