Skip to content

Instantly share code, notes, and snippets.

@DeafMan1983
Last active July 1, 2022 14:30
Show Gist options
  • Save DeafMan1983/86934b03b701de37a39acb0163ce391a to your computer and use it in GitHub Desktop.
Save DeafMan1983/86934b03b701de37a39acb0163ce391a to your computer and use it in GitHub Desktop.
That is an example Menu, Game and Option like scene switcher with old version of Half-Life by Serria or Quake by IQ Software, It was porting from C++/C in StackOverflow https://stackoverflow.com/questions/39133873/how-to-set-a-gui-button-in-the-win32-window-using-sdl-c?answertab=active#tab-top - It is simple result . If you add Text and sprites …
using System;
using static SDL2.SDL;
namespace SdlMenuExample
{
static class Program
{
public struct color
{
public byte r, g, b, a;
}
public struct button_t
{
public SDL_Rect rect;
public color color;
public bool pressed;
}
public enum State
{
Menu = 0,
Game = 1,
Option = 2
}
// Important "ref" before button_t because it won't work clickable.
static void button_process_event(ref button_t button, SDL_Event evt)
{
if(evt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN)
{
if (evt.button.button == SDL_BUTTON_LEFT &&
// If you use without "=" after > and < than it works same.
evt.button.x >= button.rect.x &&
evt.button.x <= (button.rect.x + button.rect.w) &&
evt.button.y >= button.rect.y &&
evt.button.y <= (button.rect.y + button.rect.h))
{
button.pressed = true;
}
}
}
static bool button_click(IntPtr renderer, ref button_t button)
{
SDL_SetRenderDrawColor(renderer, button.color.r, button.color.g, button.color.b, button.color.a);
SDL_RenderFillRect(renderer, ref button.rect);
if (button.pressed)
{
button.pressed = false;
return true;
}
return false;
}
static void Main()
{
SDL_Init(SDL_INIT_EVERYTHING);
IntPtr sdL_window = SDL_CreateWindow("Hello SDL2", 100, 100, 400, 400, SDL_WindowFlags.SDL_WINDOW_SHOWN);
IntPtr sdl_renderer = SDL_CreateRenderer(sdL_window, -1, SDL_RendererFlags.SDL_RENDERER_SOFTWARE);
// New Game
button_t btn_game = new button_t
{
color = new color
{
r = 255 / 4,
g = 255 / 4,
b = 255 / 4,
a = 255
},
rect = new SDL_Rect
{
x = 8,
y = 8,
w = 64,
h = 32
}
};
// Option
button_t btn_option = new button_t
{
color = new color
{
r = 255 / 4,
g = 255 / 4,
b = 255 / 4,
a = 255
},
rect = new SDL_Rect
{
x = 80,
y = 8,
w = 64,
h = 32
}
};
// Return
button_t btn_return = new button_t
{
color = new color
{
r = 255 / 4,
g = 255 / 4,
b = 255 / 4,
a = 255
},
rect = new SDL_Rect
{
x = 8,
y = 8,
w = 64,
h = 32
}
};
State state = 0;
bool isQuit = true;
while(isQuit)
{
while (SDL_PollEvent(out SDL_Event evt) != 0)
{
if (evt.type == SDL_EventType.SDL_QUIT)
{
isQuit = false;
}
if (state == State.Menu)
{
button_process_event(ref btn_game, evt);
button_process_event(ref btn_option, evt);
}
else if (state == State.Game || state == State.Option)
{
button_process_event(ref btn_return, evt);
}
}
SDL_SetRenderDrawColor(sdl_renderer, 255, 255 / 4, 0, 255);
SDL_RenderClear(sdl_renderer);
if (state == State.Menu)
{
if (button_click(sdl_renderer, ref btn_game))
{
Console.WriteLine("Pressed!");
state = State.Game;
}
if (button_click(sdl_renderer, ref btn_option))
{
Console.WriteLine("Pressed!");
state = State.Option;
}
}
else if (state == State.Game)
{
SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);
SDL_RenderClear(sdl_renderer);
if (button_click(sdl_renderer, ref btn_return))
{
Console.WriteLine("Pressed!");
state = State.Menu;
}
}
else if (state == State.Option)
{
SDL_SetRenderDrawColor(sdl_renderer, 255, 0, 0, 255);
SDL_RenderClear(sdl_renderer);
if (button_click(sdl_renderer, ref btn_return))
{
Console.WriteLine("Pressed!");
state = State.Menu;
}
}
SDL_RenderPresent(sdl_renderer);
}
SDL_DestroyRenderer(sdl_renderer);
SDL_DestroyWindow(sdL_window);
SDL_Quit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment