Skip to content

Commit 5784be1

Browse files
committed
Added example.
1 parent 40fd435 commit 5784be1

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

Example/Example.csproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\Hexa.NET.SDL2.Image\Hexa.NET.SDL2.Image.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="icon.png">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>

Example/Program.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using Hexa.NET.SDL2;
3+
using Hexa.NET.SDL2.Image;
4+
5+
unsafe
6+
{
7+
if (SDL.SDLInit(SDL.SDL_INIT_VIDEO) < 0)
8+
{
9+
Console.WriteLine("SDL could not initialize! SDL_Error: " + SDL.SDLGetErrorS());
10+
return;
11+
}
12+
13+
SDLImage.IMGInit((int)IMGInitFlags.Png);
14+
15+
var window = SDL.SDLCreateWindow("SDL Image Example", (int)SDL.SDL_WINDOWPOS_CENTERED_MASK, (int)SDL.SDL_WINDOWPOS_CENTERED_MASK, 600, 600, (uint)SDLWindowFlags.Shown);
16+
if (window == null)
17+
{
18+
Console.WriteLine("Window could not be created! SDL_Error: " + SDL.SDLGetErrorS());
19+
SDL.SDLQuit();
20+
return;
21+
}
22+
23+
SDLRenderer* renderer = SDL.SDLCreateRenderer(window, -1, (uint)SDLRendererFlags.Accelerated);
24+
if (renderer == null)
25+
{
26+
Console.WriteLine("Renderer could not be created! SDL_Error: " + SDL.SDLGetErrorS());
27+
SDL.SDLDestroyWindow(window);
28+
SDL.SDLQuit();
29+
return;
30+
}
31+
32+
// Load an image as an SDL surface
33+
SDLSurface* surface = SDLImage.IMGLoad("icon.png");
34+
if (surface == null)
35+
{
36+
Console.WriteLine("Unable to load image! SDL_image Error: ");
37+
SDL.SDLDestroyRenderer(renderer);
38+
SDL.SDLDestroyWindow(window);
39+
SDL.SDLQuit();
40+
return;
41+
}
42+
43+
// Create a texture from the surface
44+
SDLTexture* texture = SDL.SDLCreateTextureFromSurface(renderer, surface);
45+
SDL.SDLFreeSurface(surface);
46+
47+
if (texture == null)
48+
{
49+
Console.WriteLine("Unable to create texture! SDL_Error: " + SDL.SDLGetErrorS());
50+
SDL.SDLDestroyRenderer(renderer);
51+
SDL.SDLDestroyWindow(window);
52+
SDL.SDLQuit();
53+
return;
54+
}
55+
56+
// Clear the screen
57+
SDL.SDLRenderClear(renderer);
58+
59+
// Render the texture
60+
SDL.SDLRenderCopy(renderer, texture, null, null);
61+
62+
// Update the screen
63+
SDL.SDLRenderPresent(renderer);
64+
65+
// Wait for a few seconds
66+
SDL.SDLDelay(5000);
67+
68+
// Clean up
69+
SDL.SDLDestroyTexture(texture);
70+
SDL.SDLDestroyRenderer(renderer);
71+
SDL.SDLDestroyWindow(window);
72+
SDL.SDLQuit();
73+
}

Example/icon.png

201 KB
Loading

Hexa.NET.SDL2.Image.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator", "Generator\Gene
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hexa.NET.SDL2.Image", "Hexa.NET.SDL2.Image\Hexa.NET.SDL2.Image.csproj", "{6F5344E3-1715-47CA-B54B-29A45E008310}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{67BC95BA-C62B-4976-94BD-75937496BB88}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{6F5344E3-1715-47CA-B54B-29A45E008310}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{6F5344E3-1715-47CA-B54B-29A45E008310}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{6F5344E3-1715-47CA-B54B-29A45E008310}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{67BC95BA-C62B-4976-94BD-75937496BB88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{67BC95BA-C62B-4976-94BD-75937496BB88}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{67BC95BA-C62B-4976-94BD-75937496BB88}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{67BC95BA-C62B-4976-94BD-75937496BB88}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)