summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/libndsfpc/examples/gl2d/dual_screen/dual_screen.pp
blob: 9019bad0c83d089063492bf33ec7634d8b4bed23 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
(*
  Easy GL2D
  Relminator 2011 
  Richard Eric M. Lope BSN RN
  Http://Rel.Phatcode.Net
  A very small, simple, yet very fast DS 2D rendering lib using the DS' 3D core.
  --
  Translated in Object Pascal by Francesco Lombardi - 2012
  http://itaprogaming.free.fr
*)
program dual_screen;

{$mode objfpc}{$H+}

uses
  nds9, ctypes, gl2d;

const
  HALF_WIDTH  = (SCREEN_WIDTH  div 2);
  HALF_HEIGHT = (SCREEN_HEIGHT div 2);
  BRAD_PI     = (1 shl 14);


// Simple box, triangle, and putpixel demo
procedure simple(frame: cint);
var
  red, green, blue: cint;
  i: integer;
  x, y: integer;
begin
	// set up GL2D for 2d mode
	glBegin2D();

		// Do some funky color cycling
		red   := abs(sinLerp(frame * 220) * 31) shr 12;
		green := abs(sinLerp(frame * 140) * 31) shr 12;
		blue  := abs(sinLerp(frame *  40) * 31) shr 12;
		
		// fill the whole screen with a gradient box
		glBoxFilledGradient( 0, 0, 255, 191,
							 RGB15( red,  green,  blue ),
							 RGB15(  blue, 31 - red,  green ),
							 RGB15( green,  blue, 31 - red ),
							 RGB15(  31 - green, red, blue )
						   );
		
		// draw a black box
		glBoxFilled( 200, 10,
					       250, 180,
					       RGB15(0,0,0)
				       );
		
		// draw a border around the black box
		glBox( 200, 10,
			     250, 180,
			     RGB15(0,31,0)
		     );
	
		// draw a triangle
		glTriangleFilled( 20, 100,
						          200, 30,
						          60, 40,
						          RGB15(31,0,31)
						        );
	
		// draw a gradient triangle
		glTriangleFilledGradient( 20, 100,
								  200, 30,
								  60, 40,
								  RGB15(blue,red,green),
								  RGB15(green,blue, red),
								  RGB15(red,green,blue)
								);

		// translucent mode
		// Poly ID 1
		glPolyFmt(POLY_ALPHA(16) or POLY_CULL_NONE or POLY_ID(1));
		glBoxFilledGradient( 10, 50, 230, 150,
							 RGB15( green,  0,  0 ),
							 RGB15(  0, red,  0 ),
							 RGB15( 31,  0, blue ),
							 RGB15(  0, red, 31 )
						   );

		// translucent mode
		// Poly ID 2
		glPolyFmt(POLY_ALPHA(16) or POLY_CULL_NONE or POLY_ID(2));
		glTriangleFilledGradient( 70, 10,
								  20, 130,
								  230, 180,
								  RGB15(red,green,blue),
								  RGB15(blue,red,green),
								  RGB15(green,blue, red)
								);
		i := 0;
		// restore to normal(solid) rendering
		glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE or POLY_ID(3));
		// draw a circle using putpixel
{
    for i := 0 to 128 do
    begin
			x := sar(cosLerp(i * 256) * 80, 12);
			y := sar(sinLerp(i * 256) * 70, 12);
			glPutPixel( HALF_WIDTH  + x, 
                  HALF_HEIGHT + y, 
                  RGB15(red, green, blue) 
                );
    end;
    }

		while i < BRAD_PI * 2 do
		begin
			x := SarLongint(cosLerp(i) * 80, 12);
			y := SarLongint(sinLerp(i) * 70, 12);
			glPutPixel( HALF_WIDTH + x, HALF_HEIGHT + y, RGB15(red, green, blue) );
			inc(i, 256);
		end;

	glEnd2D();
end;


// oldskool lines demo
procedure lines(frame: cint);
var
  red, green, blue: cint;
  i: cint;
  px, py, px2, py2: cint;
begin
	// Do some funky color cycling
	red   := abs(sinLerp(frame * 220) * 31) shr 12 ;
	green := abs(sinLerp(frame * 140) * 31) shr 12 ;
	blue  := abs(sinLerp(frame *  40) * 31) shr 12 ;
	
	// set up GL2D for 2d mode
	glBegin2D();
		// draw a bunch (4096/32) of colored lines
		// using some funky trig equations

		i := frame;
		while i < ((1 shl 12) + frame) do
		begin
			px  := SarLongint(sinLerp(frame * 130) * 130, 12) * cosLerp( (i * 100));
			py  := SarLongint(sinLerp(frame * 280) *  70, 12) * sinLerp( (i * 200));
			px2 := SarLongint(sinLerp(frame * 330) * 100, 12) * cosLerp(((i * 300 + BRAD_PI)));
		 	py2 := SarLongint(sinLerp(frame * 140) *  80, 12) * sinLerp(((i * 400 + BRAD_PI)));
			glLine( HALF_WIDTH + SarLongint(px, 12), HALF_HEIGHT + SarLongint(py, 12),
					    HALF_WIDTH + SarLongint(px2, 12), HALF_HEIGHT + SarLongint(py2, 12),
					    RGB15(red, green, blue)
				    );
			glLine( HALF_WIDTH + SarLongint(py2, 12), HALF_HEIGHT + SarLongint(px, 12),
					    HALF_WIDTH + SarLongint(py, 12), HALF_HEIGHT + SarLongint(px2, 12),
					    RGB15(green, blue, red)
				    );
			inc(i, 32);
		end;


	glEnd2D();
end;


// Some radially displaced pixels
procedure pixels(frame: cint);
var
  radius, red, green, blue: cint;
  i, angle: cint;
  x, y, a2,x2,y2: cint;
begin
	// Elastic radius
	radius := 40 + (abs(sinLerp(frame * 20) * 80) shr 12);
	
	// Do some funky color cycling
	red := abs(sinLerp(frame * 220) * 31) shr 12 ;
	green := abs(sinLerp(frame * 140) * 31) shr 12 ;
	blue := abs(sinLerp(frame * 40) * 31) shr 12 ;

	// speed opf animation
	i := (frame * 140) and 32767;
	
	// duh!
	angle := 0;
	
	// set up GL2D for 2d mode
	glBegin2D();
		// Draw a full revolution of some radially dispalced pixels
		for angle := 0 to 512 do
		begin
			a2 := (angle * 64) + i;
			x := cosLerp(angle * 64 * 2) * radius;
			y := sinLerp(x div 32 + a2) * radius;
			x := cosLerp((y div 64) + (angle * 64)) * (radius + 20);
			y := sinLerp(x div 64 + a2) * radius;
			x2 := -y;
			y2 := x;
			
			glPutPixel( HALF_WIDTH  + SarLongint(x, 12), 
						      HALF_HEIGHT + SarLongint(y, 12), 
						      RGB15(red, green, blue)
					      );
			glPutPixel( HALF_WIDTH  + SarLongint(x2, 12), 
						      HALF_HEIGHT + SarLongint(y2, 12), 
						      RGB15(green, blue, red)
					      );
		end;
	glEnd2D();
end;

//-------------------------------------------------------
// set up a 2D layer construced of bitmap sprites
// this holds the image when rendering to the top screen
//-------------------------------------------------------
procedure initSubSprites();
var
  x, y, id: cint;
begin
	oamInit(oamSub, SpriteMapping_Bmp_2D_256, false);
 
	//set up a 4x3 grid of 64x64 sprites to cover the screen
	for y := 0 to 2 do
	  for x := 0 to 3 do
    begin
		  oamSub.oamMemory[id].attribute[0] := ATTR0_BMP or ATTR0_SQUARE or (64 * y);
		  oamSub.oamMemory[id].attribute[1] := ATTR1_SIZE_64 or (64 * x);
		  oamSub.oamMemory[id].attribute[2] := ATTR2_ALPHA(1) or (8 * 32 * y) or (8 * x);
		  inc(id);
    end;
 
	swiWaitForVBlank();
 
	oamUpdate(oamSub);
end;

var
  frame, demonum, key: cint;

begin	
	
	// Set it to my favorite mode
	videoSetMode(MODE_5_3D);
	videoSetModeSub(MODE_5_2D);
 
	// init oam to capture 3D scene
	initSubSprites();
 
	// sub background holds the top image when 3D directed to bottom
	bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
 
	
	// Initialize GL in 3d mode
	glScreen2D();
	
	

	
	// our ever present frame counter	
	frame := 0;
	
	demonum := 0;	// what demo are we currently viewing
	
	
	while true do
	begin
	
	
		// increment frame counter 
		inc(frame);
		
		// get input
		scanKeys();
		key := keysDown();
		
		
		// process input
		if ((key and KEY_DOWN) or (key and KEY_RIGHT) ) <> 0 then
			demonum := (demonum + 1) mod 3;
		
		if ((key and KEY_UP) or (key and KEY_LEFT)) <> 0 then
		begin
			dec(demonum);
			if (demonum < 0) then demonum := 2;
		end;
	
		// wait for capture unit to be ready
		while (REG_DISPCAPCNT^ and DCAP_ENABLE) <> 0 do;

	
		// Alternate rendering every other frame
		// Limits your FPS to 30
		if ((frame and 1) = 0) then
		begin
			lcdMainOnBottom();
			vramSetBankC(VRAM_C_LCD);
			vramSetBankD(VRAM_D_SUB_SPRITE);
			REG_DISPCAPCNT^ := DCAP_BANK(2) or DCAP_ENABLE or DCAP_SIZE(3);
		end else
		begin
			lcdMainOnTop();
			vramSetBankD(VRAM_D_LCD);
			vramSetBankC(VRAM_C_SUB_BG);
			REG_DISPCAPCNT^ := DCAP_BANK(3) or DCAP_ENABLE or DCAP_SIZE(3);
		end;
 
		// figure out what demo should be viewed
		case demonum of
			0: begin			// Alternately draw every other frame
				if ((frame and 1) = 0) then
					pixels( frame )
				else
					lines( frame );
			end;
			1: begin
				if ((frame and 1) = 0) then
					lines( frame )
				else
					pixels( frame );
			end;
			2: begin
				if ((frame and 1) = 0) then
					simple( frame )
				else
					lines( frame );
			end;
		  else begin
				if ((frame and 1) = 0) then
					pixels( frame )
				else
					lines( frame );
		  end;
		end;
		
		glFlush(0);
		swiWaitForVBlank();
		
		
	end;

end.