1 /*
2 
3 Copyright (C) 2015-2018 Night Dive Studios, LLC.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 // main test code for 2d library (remove if not building test app)
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "InitMac.h"
25 #include "ShockBitmap.h"
26 
27 #include "lg.h"
28 #include "fix.h"
29 #include "2d.h"
30 #include "2dRes.h"
31 #include "3d.h"
32 #include "pal.h"
33 #include "res.h"
34 #include "vox.h"
35 #include "kb.h"
36 #include "kbcook.h"
37 
38 
39 WindowPtr	gMainWindow;
40 
41 #define build_fix_angle(ang) ((65536*(ang))/360)
42 
43 // prototypes
44 void voxel_convert(grs_bitmap *bmp);
45 Boolean IsOptKeyDown(void);
46 Boolean IsCmdKeyDown(void);
47 Boolean IsShiftKeyDown(void);
48 
49 // globals
50 uchar pal_buf[768];
51 
52 
main(void)53 void main (void)
54 {
55  	OSErr				err;
56 	grs_screen 	*screen;
57 	Str255			str;
58 	Handle			testRes,testRes2,testRes3;
59   short 			w,h;
60 	long				time,i,j;
61 	grs_bitmap  bm1, bm2;
62 	char				temp[256];
63   grs_vertex 	v0,v1,v2,v3;
64   grs_vertex 	*points[4]= {&v0,&v1,&v2,&v3};
65 	Handle	 		pal;
66 	Handle			shade1,shade2;
67 	Rect				r;
68 	StandardFileReply	 reply;
69 	int					resNum=-1;
70 	Ptr					p1, p2;
71 	Ptr					big_buffer;
72 	FrameDesc		*fd;
73 	EventRecord evt;
74 	grs_canvas 	*off_canvas;
75 
76 	InitMac();
77 	CheckConfig();
78 	ResInit();
79 
80 	SetupWindows(&gMainWindow);								// setup everything
81 	SetupOffscreenBitmaps();
82 
83 	gr_init();
84 	gr_set_mode (GRM_640x480x8, TRUE);
85 	screen = gr_alloc_screen (grd_cap->w, grd_cap->h);
86 	gr_set_screen (screen);
87 	off_canvas = gr_alloc_canvas(BMT_FLAT8,512,480);
88 
89 	pal = GetResource('pal ',1001);
90 	BlockMove(*pal, pal_buf, 768L);
91   gr_set_pal(0, 256, pal_buf);
92 
93   HideCursor();
94 
95  	gr_alloc_ipal();
96 	gr_init_blend(1);
97   gr_clear(0xff);
98 
99 	PicHandle	pic = GetPicture(9998);
100 	if (pic)
101 	{
102 		Rect r = (*pic)->picFrame;
103 		OffsetRect(&r, -r.left, -r.top);
104 		OffsetRect(&r, 0, 300);
105 		DrawPicture(pic, &r);
106 
107 		ReleaseResource((Handle)pic);
108 	}
109 
110   g3_init(80,AXIS_RIGHT,AXIS_DOWN,AXIS_IN);
111 
112 	{
113 		WDPBRec		wdpb;
114 		FSSpec		spec;
115 		Ref				rid;
116 		RefTable	*refTbl;
117 
118 		memset(&wdpb, 0x00, sizeof(wdpb));					// Find out about the current directory
119 		PBHGetVol(&wdpb, false);
120 		err = FSMakeFSSpec(wdpb.ioWDVRefNum, wdpb.ioWDDirID, "objart.rsrc", &spec);
121 		resNum = ResOpenFile(&spec);
122 		if (resNum != -1)
123 		{
124 			rid = MKREF(1350, 0);
125  			refTbl = ResReadRefTable(REFID(rid));
126 
127 			rid = MKREF(1350,1631);										// Bitmap
128 			p1 = NewPtrClear(RefSize(refTbl,REFINDEX(rid)));
129   		RefExtract(refTbl, rid, p1);
130 			fd = (FrameDesc *)p1;
131 			fd->bm.bits = (uchar *)(fd+1);
132 			bm1 = fd->bm;
133 			gr_scale_ubitmap(&bm1, 0, 40, bm1.w * 2, bm1.h * 2);
134 
135 			rid = MKREF(1350,1632);										// Heights
136 			p2 = NewPtrClear(RefSize(refTbl,REFINDEX(rid)));
137   		RefExtract(refTbl, rid, p2);
138 			fd = (FrameDesc *)p2;
139 			fd->bm.bits = (uchar *)(fd+1);
140 			bm2 = fd->bm;
141 			gr_scale_ubitmap(&bm2, 0, 160, bm2.w * 2, bm2.h * 2);
142 			voxel_convert(&bm2);
143 
144 			ResCloseFile(resNum);
145 		}
146 	}
147 
148   // Voxel stuff
149 	kb_startup(NULL);
150 	kb_set_flags(KBF_BLOCK);
151   {
152 		vxs_vox 		vvv;
153 		g3s_vector	vec;
154 		g3s_angvec	ang;
155 		int					dx = 0, dy = 0, dz = 0;
156 		fix					spc = fix_make(0,0x1000);
157 		fix					siz = fix_make(0,0x0400);
158 		fix					vecX = 0, vecY = 0, vecZ = 0;
159 		fix					vec2X = 0, vec2Y = 0;
160 		ushort 			cooked = 0;
161 		uchar 				result;
162 		Boolean			done = FALSE;
163 		Rect				clrRect;
164 		Boolean			optDown, cmdDown, shiftDown;
165 
166 		SetRect(&clrRect, 128, 0, 640, 480);
167 		vx_init(16);
168 		do
169 		{
170 			gr_set_canvas(off_canvas);
171 			gr_clear(0xff);
172 
173 		  g3_start_frame();
174 			vec.gX = vecX;
175 			vec.gY = vecY;
176 			vec.gZ = vecZ;
177 			ang.tx = 0;
178 			ang.ty = 0;
179 			ang.tz = 0;
180 	   	g3_set_view_angles(&vec, &ang, ORDER_YXZ, g3_get_zoom('X',build_fix_angle(80),640,480));
181 
182 			vec.gX = vec2X;
183 			vec.gY = vec2Y;
184 			vec.gZ = fix_make(2,0);
185 			g3_start_object_angles_xyz(&vec,
186 				build_fix_angle(dx), build_fix_angle(dy), build_fix_angle(dz), ORDER_YXZ);
187 
188 			vx_init_vox(&vvv, spc, siz, 16, &bm1, &bm2);
189 			// PaintRect(&clrRect);
190 			vx_render(&vvv);
191 
192 			g3_end_object();
193 			g3_end_frame();
194 
195 			gr_set_canvas(grd_screen_canvas);
196 			gr_bitmap(&off_canvas->bm,128,0);
197 
198 			optDown = IsOptKeyDown();
199 			cmdDown = IsCmdKeyDown();
200 			shiftDown = IsShiftKeyDown();
201 				if (kb_state(0x0C))
202 					done = TRUE;
203 				else
204 				{
205 					if (kb_state(0x7B))
206 					{
207 							if (shiftDown)
208 								vecX -= 0x0600;
209 							else if (optDown)
210 								dz -= 5;												// If alt key down, change dz
211 							else
212 								dy += 5;
213 					}
214 					if (kb_state(0x7C))
215 					{
216 							if (shiftDown)
217 								vecX += 0x0600;
218 							else if (optDown)
219 								dz += 5;												// If alt key down, change dz
220 							else
221 								dy -= 5;
222 					}
223 					if (kb_state(0x7D))
224 					{
225 							if (shiftDown)
226 								vecY += 0x0600;
227 							else if (cmdDown)
228 								spc -= 0x0080;									// If cmd key down, change spacing
229 							else if (optDown)
230 								siz -= 0x0100;									// If alt key down, change size
231 							else
232 								dx += 5;												// else rotate.
233 					}
234 					if (kb_state(0x7E))
235 					{
236 							if (shiftDown)
237 								vecY -= 0x0600;
238 							else if (cmdDown)
239 								spc += 0x0080;									// If cmd key down, change spacing
240 							else if (optDown)
241 								siz += 0x0100;									// If alt key down, change size
242 							else
243 								dx -= 5;
244 					}
245 			}
246 		} while (!done);
247 	}
248 	kb_flush();
249 	kb_close();
250 
251 // {EventRecord evt; while (true){if (GetNextEvent(mDownMask+keyDownMask,&evt))
252 //  {if (evt.what==mouseDown) break;}}}
253 
254 	vx_close();
255 	gr_close();
256   ShowCursor();
257 	CleanupAndExit();
258 }
259 
260 
voxel_convert(grs_bitmap * bmp)261 void voxel_convert(grs_bitmap *bmp)
262 {
263    int x,y;
264    for (x=0; x < bmp->w; x++)
265       for (y=0; y < bmp->h; y++)
266          if (bmp->bits[(y * bmp->w) + x] != 0)
267             bmp->bits[(y * bmp->w) + x] -= 208;
268 }
269 
IsShiftKeyDown(void)270 Boolean IsShiftKeyDown(void)
271  {
272 	long		keys[4];
273 
274 	GetKeys(keys);
275 	return((keys[1] & 0x00000001) != 0L);
276  }
277 
IsOptKeyDown(void)278 Boolean IsOptKeyDown(void)
279  {
280 	long		keys[4];
281 
282 	GetKeys(keys);
283 	return((keys[1] & 0x00000004) != 0L);
284  }
285 
IsCmdKeyDown(void)286 Boolean IsCmdKeyDown(void)
287  {
288 	long		keys[4];
289 
290 	GetKeys(keys);
291 	return((keys[1] & 0x00008000) != 0L);
292  }
293