Skip to content

Commit

Permalink
Added example.
Browse files Browse the repository at this point in the history
  • Loading branch information
JunaMeinhold committed Aug 14, 2024
1 parent 40fd435 commit 5784be1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Example/Example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Hexa.NET.SDL2.Image\Hexa.NET.SDL2.Image.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="icon.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
73 changes: 73 additions & 0 deletions Example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// See https://aka.ms/new-console-template for more information
using Hexa.NET.SDL2;
using Hexa.NET.SDL2.Image;

unsafe
{
if (SDL.SDLInit(SDL.SDL_INIT_VIDEO) < 0)
{
Console.WriteLine("SDL could not initialize! SDL_Error: " + SDL.SDLGetErrorS());
return;
}

SDLImage.IMGInit((int)IMGInitFlags.Png);

var window = SDL.SDLCreateWindow("SDL Image Example", (int)SDL.SDL_WINDOWPOS_CENTERED_MASK, (int)SDL.SDL_WINDOWPOS_CENTERED_MASK, 600, 600, (uint)SDLWindowFlags.Shown);
if (window == null)
{
Console.WriteLine("Window could not be created! SDL_Error: " + SDL.SDLGetErrorS());
SDL.SDLQuit();
return;
}

SDLRenderer* renderer = SDL.SDLCreateRenderer(window, -1, (uint)SDLRendererFlags.Accelerated);
if (renderer == null)
{
Console.WriteLine("Renderer could not be created! SDL_Error: " + SDL.SDLGetErrorS());
SDL.SDLDestroyWindow(window);
SDL.SDLQuit();
return;
}

// Load an image as an SDL surface
SDLSurface* surface = SDLImage.IMGLoad("icon.png");
if (surface == null)
{
Console.WriteLine("Unable to load image! SDL_image Error: ");
SDL.SDLDestroyRenderer(renderer);
SDL.SDLDestroyWindow(window);
SDL.SDLQuit();
return;
}

// Create a texture from the surface
SDLTexture* texture = SDL.SDLCreateTextureFromSurface(renderer, surface);
SDL.SDLFreeSurface(surface);

if (texture == null)
{
Console.WriteLine("Unable to create texture! SDL_Error: " + SDL.SDLGetErrorS());
SDL.SDLDestroyRenderer(renderer);
SDL.SDLDestroyWindow(window);
SDL.SDLQuit();
return;
}

// Clear the screen
SDL.SDLRenderClear(renderer);

// Render the texture
SDL.SDLRenderCopy(renderer, texture, null, null);

// Update the screen
SDL.SDLRenderPresent(renderer);

// Wait for a few seconds
SDL.SDLDelay(5000);

// Clean up
SDL.SDLDestroyTexture(texture);
SDL.SDLDestroyRenderer(renderer);
SDL.SDLDestroyWindow(window);
SDL.SDLQuit();
}
Binary file added Example/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Hexa.NET.SDL2.Image.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator", "Generator\Gene
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hexa.NET.SDL2.Image", "Hexa.NET.SDL2.Image\Hexa.NET.SDL2.Image.csproj", "{6F5344E3-1715-47CA-B54B-29A45E008310}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{67BC95BA-C62B-4976-94BD-75937496BB88}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{6F5344E3-1715-47CA-B54B-29A45E008310}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F5344E3-1715-47CA-B54B-29A45E008310}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F5344E3-1715-47CA-B54B-29A45E008310}.Release|Any CPU.Build.0 = Release|Any CPU
{67BC95BA-C62B-4976-94BD-75937496BB88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67BC95BA-C62B-4976-94BD-75937496BB88}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67BC95BA-C62B-4976-94BD-75937496BB88}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67BC95BA-C62B-4976-94BD-75937496BB88}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 5784be1

Please sign in to comment.