KumiScript/gearbox/GameStateMain.cs

118 lines
No EOL
4.1 KiB
C#

using SDL2;
using KumiScript.Renderer;
using TextBox = KumiScript.Renderer.TextBox;
namespace KumiScript.Gearbox
{
public class GameStateMain : GameState
{
List<Scene> _scenes;
Scene _curScene;
List<Sprite> _sprites;
List<Background> _backgrounds;
List<ManagedFont> _fonts;
GraphicalUserInterface _graphicalUI;
TextBox _bigBox;
SDLRenderer _renderer;
SDL.SDL_Event e;
ulong _tA;
ulong _tB;
float _delta;
public GameStateMain(SDLRenderer renderer)
{
_renderer = renderer;
_curScene = new Scene();
_scenes = new List<Scene>
{
_curScene
};
Sprite seiba = new Sprite("Resources/Sprites/test.png", renderer);
_sprites = new List<Sprite> {
seiba
};
Background background = new Background("Resources/Backgrounds/test.png", renderer);
_backgrounds = new List<Background>
{
background
};
_curScene.AddElement(background);
_curScene.AddElement(seiba);
_graphicalUI = new GraphicalUserInterface();
SDL.SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;
color.a = 0;
ManagedFont font = new ManagedFont("Resources/Fonts/DejaVuSerif.ttf", 36, color, renderer, 256); //ascii
_fonts = new List<ManagedFont> {
font
};
Text hello = new Text("Hello, world!", font);
Text lorem = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", font);
_bigBox = new TextBox("Resources/Interface/bigbox.png", renderer, 36, true);
_bigBox.AddText(hello);
_bigBox.BreakLine();
_bigBox.AddText(lorem);
_graphicalUI.AddElement(_bigBox);
_tA = SDL.SDL_GetPerformanceCounter();
_tB = SDL.SDL_GetPerformanceCounter();
_delta = 0;
}
public override void AcceptMessage(StateMessage message)
{
throw new NotImplementedException();
}
public override bool IsQuitting()
{
return false;
}
public override GameState UpdateState()
{
_tA = SDL.SDL_GetPerformanceCounter();
while (SDL.SDL_PollEvent(out e) > 0)
{
switch (e.type)
{
case SDL.SDL_EventType.SDL_QUIT:
CleanUp();
return new GameStateQuitting();
case SDL.SDL_EventType.SDL_KEYUP:
switch (e.key.keysym.sym)
{
case SDL.SDL_Keycode.SDLK_RETURN:
_bigBox.UpdateState();
break;
}
break;
}
}
_tB = _tA;
_renderer.Clear();
_curScene.DrawScene();
_graphicalUI.DrawInterface();
_renderer.SwapBuffers();
_tB = SDL.SDL_GetPerformanceCounter();
_delta = (_tB - _tA) / SDL.SDL_GetPerformanceFrequency() * 1000.0f;
SDL.SDL_Delay((uint) (1000/60 - _delta));
return this;
}
private void CleanUp()
{
foreach (Sprite s in _sprites)
s.Dispose();
foreach(Background b in _backgrounds)
b.Dispose();
foreach(ManagedFont f in _fonts)
f.Dispose();
_graphicalUI.Dispose();
}
}
}