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.3PawnrenderingProject Included

main.pwn

Example: main.pwn

Examples / SDK 6.3 / 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//Screen pat callback.
125public ON_Pat(const count, const display, const bool:opposite)
126{
127 if (SELF_ID == 0)
128 {
129 }
130 else
131 {
132 }
133}
134
135//Application quit callback.
136public ON_Quit()
137{
138 //
139 //Save game data here
140 //
141}
142
143// Functions
144//----------------
145
146//Returns the squared length of a distance vector between two points
147distanceSquared(Fixed:x1,Fixed:y1,Fixed:x2,Fixed:y2)
148{
149 return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
150}
151
152//Initializes particle arrays for given attractor
153createParticles(Fixed:x,Fixed:y, magnet)
154{
155 for(new i=0;i<PARTICLES_PER_MAGNET;i++)
156 {
157 new Fixed:r1 = fixed(RND_randomize(0,100))/100.0;
158 new Fixed:r2 = fixed(RND_randomize(0,100))/100.0;
159
160 particles[numParticles].x = x;
161 particles[numParticles].y = y;
162 particles[numParticles].shx = x;
163 particles[numParticles].shy = y;
164
165 particles[numParticles].angle = 0;
166 particles[numParticles].size = 0+r1*6.5;
167 particles[numParticles].speed = (0.001 + ((particles[numParticles].size+1)/40.0)*0.015);
168 particles[numParticles].force = 0.98 - r2*0.15;
169 particles[numParticles].orbit = 1.0;
170
171 particles[numParticles].magnet = magnet;
172
173 numParticles++;
174 }
175}
176
177//Creates an attractor at given coordinates
178createMagnet(Fixed:x,Fixed:y)
179{
180 if(numMagnets<NUM_MAGNETS)
181 {
182 magnets[numMagnets].x = x;
183 magnets[numMagnets].y = y;
184 magnets[numMagnets].orbit = 100.0;
185 magnets[numMagnets].size = 1.0;
186
187 createParticles(x,y,numMagnets);
188
189 numMagnets++;
190 }
191}
192
193//Animates attractor positions
194moveMagnets()
195{
196 new Fixed:fSin = fixed(FixedSin( magnetsShift))/256.0;
197
198 for(new i=0;i<numMagnets;i++)
199 {
200 magnets[i].x+=fixed(FixedSin( magnetsShift*(i+1)))/512.0;
201 }
202 magnetsShift++;
203 if(magnetsShift>360) magnetsShift=0;
204}
205
206//Handles particle system and renders particles with circles
207renderParticles()
208{
209 new position[GFX_POINT];
210
211 for(new i=0;i<numParticles;i++)
212 {
213 new Fixed:currentDistance = -1;
214 new Fixed:closestDistance = -1;
215 new closestMagnet = -1;
216
217 // Attraction force vector
218 new force[Fixed:.x,Fixed:.y];
219 force.x = 0;
220 force.y = 0;
221
222 // Figure out the most influential attractor for a particle and calculate attraction force
223 for(new j=0;j<numMagnets;j++)
224 {
225 currentDistance = distanceSquared(particles[i].x,particles[i].y,magnets[j].x,magnets[j].y) - magnets[j].orbit*magnets[j].orbit;
226
227 if(particles[i].magnet!=j)
228 {
229 new Fixed:fx = magnets[j].x - particles[i].x;
230
231 if( fx > -MAGNETIC_FORCE_THRESHOLD && fx < MAGNETIC_FORCE_THRESHOLD )
232 {
233 force.x += fx / MAGNETIC_FORCE_THRESHOLD;
234 }
235
236 new Fixed:fy = magnets[j].y - particles[i].y;
237 if( fy > -MAGNETIC_FORCE_THRESHOLD && fy < MAGNETIC_FORCE_THRESHOLD )
238 {
239 force.y += fy / MAGNETIC_FORCE_THRESHOLD;
240 }
241
242 }
243
244 if( closestMagnet == -1 || currentDistance < closestDistance )
245 {
246 closestDistance = currentDistance;
247 closestMagnet = j;
248 }
249
250 }
251
252 if( particles[i].magnet == -1 || particles[i].magnet != closestMagnet )
253 {
254 particles[i].magnet = closestMagnet;
255 }
256
257 // Spin the particle
258 particles[i].angle += particles[i].speed*0.5;
259
260 // Translate the particle towards the magnet position
261 particles[i].shx += ( (magnets[closestMagnet].x+(force.x*4)) - particles[i].shx) * particles[i].speed*0.5;
262 particles[i].shy += ( (magnets[closestMagnet].y+(force.y*4)) - particles[i].shy) * particles[i].speed*0.5;
263
264 new Fixed:fSin = fixed(FixedSin(i+particles[i].angle))/256.0;
265 new Fixed:fCos = fixed(FixedCos(i+particles[i].angle))/256.0;
266
267 // Apply integral change to the particle's position
268 particles[i].x = particles[i].shx + fSin * (particles[i].orbit*particles[i].force);
269 particles[i].y = particles[i].shy + fCos * (particles[i].orbit*particles[i].force);
270
271 // Slowly inherit the cloest magnets orbit
272 particles[i].orbit += ( magnets[closestMagnet].orbit - particles[i].orbit )* 0.1;
273
274 // Change particle color
275 new color = fint(255.0/(particles[i].size+1));
276
277 // Draw particle
278
279 position.x = fint(particles[i].x);
280 position.y = fint(particles[i].y);
281
282 GFX_drawSolidCircle(position, fint(particles[i].size)*3, GFX_fromARGB8888(255,color, color*0.8, color*0.9));
283 }
284}
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.3 / Pawn / rendering
info.json
Examples / SDK 6.3 / Pawn / rendering / Simple Shapes - Circles
wowcubeapp-build.json
Examples / SDK 6.3 / Pawn / rendering / Simple Shapes - Circles / project
Simple Shapes - Points
Examples / SDK 6.3 / Pawn / rendering
Previous Node
info.json
Examples / SDK 6.3 / Pawn / rendering / Simple Shapes - Circles
Next Node
wowcubeapp-build.json
Examples / SDK 6.3 / Pawn / rendering / Simple Shapes - Circles / project