-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsdlapp.nim
More file actions
144 lines (126 loc) · 4.69 KB
/
Copy pathsdlapp.nim
File metadata and controls
144 lines (126 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import std/[os, strutils]
import sdl3_nim
#--- Add application icon
when defined(windows):
when not defined(vcc): # imguinVcc.res TODO WIP
include ./res/resource
const MainWinWidth = 530
const MainWinHeight = 530
const Speed1 = 0.3
const StartupDelay = 90 # 60 frame
var
window: ptr SDL_Window = nil
renderer: ptr SDL_Renderer = nil
angle: cdouble = 0
speed = Speed1
delayAtStartup = StartupDelay
#-------------------
#--- png as textrue
#-------------------
const ImageNames = ["1a.png", "2a.png", "3a.png", "4a.png"]
var
textures: array[ImageNames.len, ptr SDL_Texture]
textureWidth: cfloat
textureHeight: cfloat
#----------------
#--- SDL_AppInit
#----------------
proc SDL_AppInit*(appstate: ptr pointer, argc: cint, argv: ptr UncheckedArray[cstring]): SDL_AppResult {.cdecl.} =
SDL_SetAppMetadata("Example Renderer Textures", "1.0", "sdl3_nim")
if not SDL_Init(SDL_INIT_VIDEO):
SDL_Log_proc("Couldn't initialize SDL: %s", SDL_GetError())
return SDL_APP_FAILURE;
if not SDL_CreateWindowAndRenderer("SDL3: SDL_App Demo", MainWinWidth, MainWinHeight, SDL_WINDOW_RESIZABLE, addr window, addr renderer):
SDL_Log_proc("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE
if not SDL_SetRenderVSync(renderer, 1):
SDL_Log_proc("Fail!: VSync setting : %s", SDL_GetError())
for i, imageName in ImageNames:
const funcname = "SDL_LoadPNG()"
let surface = SDL_LoadPNG(imageName.cstring)
if not isNil surface:
textures[i] = SDL_CreateTextureFromSurface(renderer, surface)
echo "$# OK !: $#" % [funcname, imageName]
SDL_GetTextureSize(textures[i], addr textureWidth, addr textureHeight)
else:
echo "Error!: $#", imageName
return SDL_APP_CONTINUE
#-------------------
#--- SDL_AppIterate
#-------------------
proc SDL_AppIterate*(appstate: pointer): SDL_AppResult {.cdecl.} =
#/* as you can see from this, rendering draws over whatever was drawn before it. */
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255)
SDL_RenderClear(renderer) #/* start with a blank canvas. */
var
w, h: cfloat
iw, ih: cint
let width = textureWidth/2
let height = textureHeight/2
SDL_GetWindowSizeInPixels(window, addr iw, addr ih)
w = iw.cfloat
h = ih.cfloat
type Attr = object
texture: ptr SDL_Texture
xs, ys: cfloat
ws, hs: cfloat
let attribs = [
Attr(texture: textures[0], xs: (w - textureWidth)/2, ys: (h - textureHeight)/2, ws: width, hs: height)
, Attr(texture: textures[1], xs: w/2, ys: (h - textureHeight)/2, ws: width, hs: height)
, Attr(texture: textures[2], xs: (w - textureWidth)/2, ys: h/2, ws: width, hs: height)
, Attr(texture: textures[3], xs: w/2, ys: h/2, ws: width, hs: height)
]
for attrib in attribs:
var rectDst = SDL_FRect(x: attrib.xs, y: attrib.ys, w: attrib.ws, h: attrib.hs)
SDL_RenderTextureRotated(renderer, attrib.texture, nil, addr rectDst, angle, nil, SDL_FLIP_NONE)
if angle < 360.0:
angle = angle + speed
else:
angle = 0
delayAtStartup = StartupDelay
if delayAtStartup > 0:
dec delayAtStartup
angle = 0
if speed == 0:
SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255); # Orange
SDL_RenderDebugText(renderer, 100, 500, "Stop")
SDL_SetRenderDrawColor(renderer, 0, 180, 0, 255); # Green
SDL_RenderDebugText(renderer, 10, 10, "Start / Stop: SPACE")
SDL_RenderDebugText(renderer, 10, 20, "Restart : R or ENTER")
#--- Render
SDL_RenderPresent(renderer)
return SDL_APP_CONTINUE # carry on with the program!
#-----------------
#--- SDL_AppEvent
#-----------------
proc SDL_AppEvent*(appstate: pointer, event: ptr SDL_Event): SDL_AppResult {.cdecl.} =
if event.type_field == SDL_EVENT_QUIT.uint32:
return SDL_APP_SUCCESS # end the program, reporting success to the OS.
if event.key.type_field == SDL_EVENT_KEY_DOWN:
if event.key.key == SDLK_R or event.key.key == SDLK_RETURN:
angle = 0
delayAtStartup = StartupDelay
if event.key.key == SDLK_SPACE:
speed = if speed == 0: Speed1 else: 0
if angle > 360:
angle = 0
speed = Speed1
return SDL_APP_CONTINUE # carry on with the program!
#----------------
#--- SDL_AppQuit
#----------------
proc SDL_AppQuit*(appstate: pointer, res: SDL_AppResult): void {.cdecl.} =
SDL_Quit_proc()
#-------------
#--- SDL_main
#-------------
proc SDL_main(argc: cint, argv: ptr UncheckedArray[cstring]): cint {.cdecl.} =
return SDL_EnterAppMainCallbacks(argc, argv, SDL_AppInit, SDL_AppIterate, SDL_AppEvent, SDL_AppQuit)
#--------------
#--- main porc
#--------------
var argv: seq[cstring]
for str in commandLineParams():
argv.add str.cstring
argv.add nil
discard SDL_RunApp(paramCount().cint, cast[ptr UncheckedArray[cstring]](unsafeAddr argv[0]), SDL_main, nil)