WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Examples
  4. main.pwn
Mission NodeSDK 6.1PawnrenderingProject Included

main.pwn

Example: main.pwn

Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Circles / project / src
main.pwn
C
1// This PAWN script is generated by WOWCube SDK project wizard
2
3#define G2D
4
5#pragma warning disable 203
6#pragma warning disable 213
7#pragma warning disable 229
8
9// Includes
10// -----------------------------
11#include "wowcore.inc"
12#include "topology.inc"
13#include "graphics.inc"
14#include "fixed.inc"
15#include "motion_sensor.inc"
16#include "math.inc"
17#include "log.inc"
18#include "string.inc"
19#include "random.inc"
20
21// Application defines
22// -----------------------------
23#define DISPLAY_CENTER_X 120
24#define DISPLAY_CENTER_Y 120
25
26#define SCREEN_WIDTH 240
27#define SCREEN_HEIGHT 240
28
29#define NUM_MAGNETS 4
30#define PARTICLES_PER_MAGNET 15
31#define MAGNETIC_FORCE_THRESHOLD 300
32
33// Application constants
34// ------------------------------
35new const Colors[.white, .black, .red, .green, .blue, .marsh] =
36[
37 0xFFFFFFFF,
38 0xFF000000,
39 0xFFFF0000,
40 0xFF00FF00,
41 0xFF0000FF,
42 0xFF161D24
43];
44
45// Global variables and constants
46// ----------------------------
47
48//array of attractors ("magnets")
49new Fixed:magnets[NUM_MAGNETS][Fixed:.x,Fixed:.y,Fixed:.size,Fixed:.orbit];
50new numMagnets = 0;
51new magnetsShift = 0;
52
53//array of particles
54new Fixed:particles[NUM_MAGNETS*PARTICLES_PER_MAGNET][Fixed:.x,Fixed:.y,Fixed:.shx,Fixed:.shy,Fixed:.angle,Fixed:.size,Fixed:.speed,Fixed:.force,.magnet,Fixed:.orbit];
55new numParticles = 0;
56
57
58// WOWCube application callbacks
59// -----------------------------
60
61//Applicaton initialization callback. Called once when CUB application starts
62public ON_Init(id, size, const pkt[])
63{
64 LOG_i("\n");
65 LOG_i("*** WOWCube SDK Example \"Simple Shapes - Circles\" ***\n\n");
66
67 //create 4 attractors
68 createMagnet(40,40);
69 createMagnet(50,180);
70 createMagnet(150,80);
71 createMagnet(120,120);
72
73 LOG_i("Done.\n");
74}
75
76//Saved application state data load callback. Gets called in response to loadState() function call
77public ON_Load(id, size, const pkt[])
78{
79}
80
81//Main run loop callback. Gets called recurrently by the CUB application as frequent as application code allows.
82public ON_Tick()
83{
84}
85
86//This callback is gets called immediately after ON_Tick(). Use it for calling your rendering code.
87public ON_Render()
88{
89 for(new screenNumber = 0; screenNumber<3; screenNumber++)
90 {
91 GFX_setRenderTarget(screenNumber);
92 GFX_clear(Colors.marsh);
93
94 //Render particles
95 renderParticles();
96
97 GFX_render();
98 }
99}
100
101//The "physics" callback. Gets called recurrently with 30ms resolution.
102public ON_PhysicsTick()
103{
104 //Slowly change positions of attractors
105 moveMagnets();
106}
107
108//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module
109public ON_Packet(type, size, const pkt[])
110{
111}
112
113//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed
114public ON_Twist(twist[TOPOLOGY_TWIST_INFO])
115{
116 if (SELF_ID == 0)
117 {
118 }
119 else
120 {
121 }
122}
123
124//Device shake detection callback.
125public ON_Shake(const count)
126{
127}
128
129//Screen tap callback.
130public ON_Tap(const count, const display, const bool:opposite)
131{
132 if (SELF_ID == 0)
133 {
134 }
135 else
136 {
137 }
138}
139
140//Application quit callback.
141public ON_Quit()
142{
143 //
144 //Save game data here
145 //
146}
147
148// Functions
149//----------------
150
151//Returns the squared length of a distance vector between two points
152distanceSquared(Fixed:x1,Fixed:y1,Fixed:x2,Fixed:y2)
153{
154 return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
155}
156
157//Initializes particle arrays for given attractor
158createParticles(Fixed:x,Fixed:y, magnet)
159{
160 for(new i=0;i<PARTICLES_PER_MAGNET;i++)
161 {
162 new Fixed:r1 = fixed(RND_randomize(0,100))/100.0;
163 new Fixed:r2 = fixed(RND_randomize(0,100))/100.0;
164
165 particles[numParticles].x = x;
166 particles[numParticles].y = y;
167 particles[numParticles].shx = x;
168 particles[numParticles].shy = y;
169
170 particles[numParticles].angle = 0;
171 particles[numParticles].size = 0+r1*6.5;
172 particles[numParticles].speed = (0.001 + ((particles[numParticles].size+1)/40.0)*0.015);
173 particles[numParticles].force = 0.98 - r2*0.15;
174 particles[numParticles].orbit = 1.0;
175
176 particles[numParticles].magnet = magnet;
177
178 numParticles++;
179 }
180}
181
182//Creates an attractor at given coordinates
183createMagnet(Fixed:x,Fixed:y)
184{
185 if(numMagnets<NUM_MAGNETS)
186 {
187 magnets[numMagnets].x = x;
188 magnets[numMagnets].y = y;
189 magnets[numMagnets].orbit = 100.0;
190 magnets[numMagnets].size = 1.0;
191
192 createParticles(x,y,numMagnets);
193
194 numMagnets++;
195 }
196}
197
198//Animates attractor positions
199moveMagnets()
200{
201 new Fixed:fSin = fixed(FixedSin( magnetsShift))/256.0;
202
203 for(new i=0;i<numMagnets;i++)
204 {
205 magnets[i].x+=fixed(FixedSin( magnetsShift*(i+1)))/512.0;
206 }
207 magnetsShift++;
208 if(magnetsShift>360) magnetsShift=0;
209}
210
211//Handles particle system and renders particles with circles
212renderParticles()
213{
214 new position[GFX_POINT];
215
216 for(new i=0;i<numParticles;i++)
217 {
218 new Fixed:currentDistance = -1;
219 new Fixed:closestDistance = -1;
220 new closestMagnet = -1;
221
222 // Attraction force vector
223 new force[Fixed:.x,Fixed:.y];
224 force.x = 0;
225 force.y = 0;
226
227 // Figure out the most influential attractor for a particle and calculate attraction force
228 for(new j=0;j<numMagnets;j++)
229 {
230 currentDistance = distanceSquared(particles[i].x,particles[i].y,magnets[j].x,magnets[j].y) - magnets[j].orbit*magnets[j].orbit;
231
232 if(particles[i].magnet!=j)
233 {
234 new Fixed:fx = magnets[j].x - particles[i].x;
235
236 if( fx > -MAGNETIC_FORCE_THRESHOLD && fx < MAGNETIC_FORCE_THRESHOLD )
237 {
238 force.x += fx / MAGNETIC_FORCE_THRESHOLD;
239 }
240
241 new Fixed:fy = magnets[j].y - particles[i].y;
242 if( fy > -MAGNETIC_FORCE_THRESHOLD && fy < MAGNETIC_FORCE_THRESHOLD )
243 {
244 force.y += fy / MAGNETIC_FORCE_THRESHOLD;
245 }
246
247 }
248
249 if( closestMagnet == -1 || currentDistance < closestDistance )
250 {
251 closestDistance = currentDistance;
252 closestMagnet = j;
253 }
254
255 }
256
257 if( particles[i].magnet == -1 || particles[i].magnet != closestMagnet )
258 {
259 particles[i].magnet = closestMagnet;
260 }
261
262 // Spin the particle
263 particles[i].angle += particles[i].speed*0.5;
264
265 // Translate the particle towards the magnet position
266 particles[i].shx += ( (magnets[closestMagnet].x+(force.x*4)) - particles[i].shx) * particles[i].speed*0.5;
267 particles[i].shy += ( (magnets[closestMagnet].y+(force.y*4)) - particles[i].shy) * particles[i].speed*0.5;
268
269 new Fixed:fSin = fixed(FixedSin(i+particles[i].angle))/256.0;
270 new Fixed:fCos = fixed(FixedCos(i+particles[i].angle))/256.0;
271
272 // Apply integral change to the particle's position
273 particles[i].x = particles[i].shx + fSin * (particles[i].orbit*particles[i].force);
274 particles[i].y = particles[i].shy + fCos * (particles[i].orbit*particles[i].force);
275
276 // Slowly inherit the cloest magnets orbit
277 particles[i].orbit += ( magnets[closestMagnet].orbit - particles[i].orbit )* 0.1;
278
279 // Change particle color
280 new color = fint(255.0/(particles[i].size+1));
281
282 // Draw particle
283
284 position.x = fint(particles[i].x);
285 position.y = fint(particles[i].y);
286
287 GFX_drawSolidCircle(position, fint(particles[i].size)*3, GFX_fromARGB8888(255,color, color*0.8, color*0.9));
288 }
289}
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Project files

main.pwn
project/src/main.pwn
wowcubeapp-build.json
project/wowcubeapp-build.json
Context Rail

Related nodes

Simple Shapes - Circles
Examples / SDK 6.1 / Pawn / rendering
info.json
Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Circles
wowcubeapp-build.json
Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Circles / project
Simple Shapes - Points
Examples / SDK 6.1 / Pawn / rendering
Previous Node
info.json
Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Circles
Next Node
wowcubeapp-build.json
Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Circles / project