summaryrefslogtreecommitdiffhomepage
path: root/src/main.c
blob: 70663a0166c3e43ca13d065577f259c192cea616 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*******************************************************************************************
 *
 *   raylib [models] example - Draw textured cube
 *
 *   Example originally created with raylib 4.5, last time updated with raylib 4.5
 *
 *   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
 *   BSD-like license that allows static linking with closed source software
 *
 *   Copyright (c) 2022-2024 Ramon Santamaria (@raysan5)
 *
 ********************************************************************************************/


#include <libdragon.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <raylib.h>
#include <rlgl.h>
#include <fmath.h>
//#include "raylib_font_RGBA16_5551.h"
#define ATTR_NINTENDO64_WIDTH 320
#define ATTR_NINTENDO64_HEIGHT 240
bool flag=true;
bool xflag=false;

#include "player.h"
#include "input.h"
#include "render.h"
#include "world.h"
#include "bullet.h"

Camera camera = { 0 };
Camera camera2 = { 0 };

void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color); // Draw cube textured
void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, float width, float height, float length, Color color); // Draw cube with a region of a texture



bool initApp()
{
	return true;
}
void finishApp()
{


}
extern Texture2D getFontGLTextureId(char *text);
Rectangle src={0.0f,0.0f,5.0f,10.0f};
Rectangle dst={0.0f,0.0f,32.0f,32.0f};


World world = {
	.players = {
		{ 
			.position = { 0 },
			.direction = { 1, 0, 0 },
			.speed = 1.0f/30.0f,
			.color = DARKBLUE,
			.camera_mode = 1,
		},
		{
			.position = { 2, 0, 0 },
			.direction = { -1, 0, 0 },
			.speed = 1.0f/30.0f,
			.color = DARKPURPLE,
			.camera_mode = 1,
		}
	}
};


//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
	// Initialization
	//--------------------------------------------------------------------------------------
	const int screenWidth = ATTR_NINTENDO64_WIDTH;
	const int screenHeight = ATTR_NINTENDO64_HEIGHT;


	InitWindow(screenWidth, screenHeight, "raylib [shapes] example - basic shapes");

	// Define the camera to look into our 3d world
	camera.position = (Vector3){ 0.0f, 10.0f, 10.0f };
	camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
	camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
	camera.fovy = 45.0f;
	camera.projection = CAMERA_PERSPECTIVE;

	// Define the camera to look into our 3d world
	camera2.position = (Vector3){ 1.0f, -10.0f, 5.0f };
	camera2.target = (Vector3){ 0.0f, 0.0f, 0.0f };
	camera2.up = (Vector3){ 0.0f, 1.0f, 0.0f };
	camera2.fovy = 35.0f;
	camera2.projection = CAMERA_PERSPECTIVE;


	Texture2D texture = LoadTexture("rom:/cubicmap_atlas32x32.png");    // Load map texture
																			   //Model model = LoadModel("rom:/plane.m3d");


	//BoundingBox bounds = GetMeshBoundingBox(model.meshes[0]);   // Set model bounds



	SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
									//----------------------------------------------------------

									// Main game loop
	while (flag)    // Detect window close with Start button
	{
		// Update
		//-----------------------------------------------------
		//controller_read(&controllers);
		updateController();

		if(pressed_p1.r)
		{
			if(world.players[0].camera_mode == 0)
			{
				world.players[0].camera_mode = 1;
			}
			else
			{
				world.players[0].camera_mode = 0;
			}
		}

		if(pressed_p2.r)
		{
			if(world.players[1].camera_mode == 0)
			{
				world.players[1].camera_mode = 1;
			}
			else
			{
				world.players[1].camera_mode = 0;
			}
		}

		if(pressed_p1.z)
		{
			spawn_bullet(1, world.players[0].position, world.players[0].direction);
		}
		if(pressed_p2.z)
		{
			spawn_bullet(2, world.players[1].position, world.players[1].direction);
		}
		bullet_collision_check();

		movePlayers();
		camera = lookThroughPlayer(camera, world.players[0]);
		camera2 = lookThroughPlayer(camera2, world.players[1]);

		// Update
		//----------------------------------------------------------------------------------
		//UpdateCamera(&camera, CAMERA_ORBITAL);
		//----------------------------------------------------------------------------------

		// Draw
		//----------------------------------------------------------------------------------
		BeginDrawing();

		ClearBackground(DARKGRAY);

		BeginScissorMode(0, 0, 320/2, 240);
		rlViewport(0, 0, 320/2, 240);
		renderWorld(&world, camera);
		EndScissorMode();
		BeginScissorMode(320/2, 0, 320/2, 240);
		rlViewport(320/2, 0, 320/2, 240);
		renderWorld(&world, camera2);
		EndScissorMode();
		rlViewport(0, 0, 320, 240);

		DrawRectangle((320/2)-4, 0, 2, 240, BLACK); // split screen line

		// Draw cube with an applied texture
		//test();
		// Draw cube with an applied texture, but only a defined rectangle piece of the texture
		//DrawCubeTextureRec(texture, (Rectangle){ 0, texture.height/2, texture.width/2, texture.height/2 }, 
		//    (Vector3){ 2.0f, 1.0f, 0.0f }, 2.0f, 2.0f, 2.0f, WHITE);
		

		char text[50];
		//sprintf(text, "Joystick %d,%d", inputs_p1.stick_x, inputs_p1.stick_y); 
		//DrawText(text, 10, 30, 12, GREEN);
		sprintf(text, "Player1: %d", world.players[0].points); 
		DrawText(text, 50, 30, 12, BLUE);
		sprintf(text, "Player2: %d", world.players[1].points); 
		DrawText(text, 210, 30, 12, VIOLET);
		/*
		DrawText("Position", 150, 30, 12, GREEN);
		DrawText(text, 150, 45, 12, GREEN);
		sprintf(text, "y: %f", camera.position.y); 
		DrawText(text, 150, 60, 12, GREEN);
		sprintf(text, "z: %f", camera.position.z); 
		DrawText(text, 150, 75, 12, GREEN);
		DrawText("Target", 220, 30, 12, GREEN);
		sprintf(text, "%f", camera.target.x); 
		DrawText(text, 220, 45, 12, GREEN);
		sprintf(text, "%f", camera.target.y); 
		DrawText(text, 220, 60, 12, GREEN);
		sprintf(text, "%f", camera.target.z); 
		DrawText(text, 220, 75, 12, GREEN);
		*/
		DrawFPS(10, 10);

		EndDrawing();



		//-----------------------------------------------------
	}

	// De-Initialization
	//--------------------------------------------------------------------------------------
	UnloadTexture(texture);     // Unload map texture

	CloseWindow();        // Close window and OpenGL context
						  //----------------------------------------------------------

	finishApp();
	return 0;
}
void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color)
{
	float x = position.x;
	float y = position.y;
	float z = position.z;

	// Set desired texture to be enabled while drawing following vertex data
	rlSetTexture(texture.id);

	// Vertex data transformation can be defined with the commented lines,
	// but in this example we calculate the transformed vertex data directly when calling rlVertex3f()
	//rlPushMatrix();
	// NOTE: Transformation is applied in inverse order (scale -> rotate -> translate)
	//rlTranslatef(2.0f, 0.0f, 0.0f);
	//rlRotatef(45, 0, 1, 0);
	//rlScalef(2.0f, 2.0f, 2.0f);

	rlBegin(RL_QUADS);
	rlColor4ub(color.r, color.g, color.b, color.a);
	// Front Face
	rlNormal3f(0.0f, 0.0f, 1.0f);       // Normal Pointing Towards Viewer
	rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2);  // Bottom Left Of The Texture and Quad
	rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2);  // Bottom Right Of The Texture and Quad
	rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z + length/2);  // Top Right Of The Texture and Quad
	rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z + length/2);  // Top Left Of The Texture and Quad
																						   // Back Face
	rlNormal3f(0.0f, 0.0f, - 1.0f);     // Normal Pointing Away From Viewer
	rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z - length/2);  // Bottom Right Of The Texture and Quad
	rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2);  // Top Right Of The Texture and Quad
	rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2);  // Top Left Of The Texture and Quad
	rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z - length/2);  // Bottom Left Of The Texture and Quad
																						   // Top Face
	rlNormal3f(0.0f, 1.0f, 0.0f);       // Normal Pointing Up
	rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2);  // Top Left Of The Texture and Quad
	rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y + height/2, z + length/2);  // Bottom Left Of The Texture and Quad
	rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y + height/2, z + length/2);  // Bottom Right Of The Texture and Quad
	rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2);  // Top Right Of The Texture and Quad
																						   // Bottom Face
	rlNormal3f(0.0f, - 1.0f, 0.0f);     // Normal Pointing Down
	rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y - height/2, z - length/2);  // Top Right Of The Texture and Quad
	rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y - height/2, z - length/2);  // Top Left Of The Texture and Quad
	rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2);  // Bottom Left Of The Texture and Quad
	rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2);  // Bottom Right Of The Texture and Quad
																						   // Right face
	rlNormal3f(1.0f, 0.0f, 0.0f);       // Normal Pointing Right
	rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z - length/2);  // Bottom Right Of The Texture and Quad
	rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2);  // Top Right Of The Texture and Quad
	rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z + length/2);  // Top Left Of The Texture and Quad
	rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2);  // Bottom Left Of The Texture and Quad
																						   // Left Face
	rlNormal3f( - 1.0f, 0.0f, 0.0f);    // Normal Pointing Left
	rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z - length/2);  // Bottom Left Of The Texture and Quad
	rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2);  // Bottom Right Of The Texture and Quad
	rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z + length/2);  // Top Right Of The Texture and Quad
	rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2);  // Top Left Of The Texture and Quad
	rlEnd();
	//rlPopMatrix();

	rlSetTexture(0);
}

// Draw cube with texture piece applied to all faces
void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, float width, float height, float length, Color color)
{
	float x = position.x;
	float y = position.y;
	float z = position.z;
	float texWidth = (float)texture.width;
	float texHeight = (float)texture.height;

	// Set desired texture to be enabled while drawing following vertex data
	rlSetTexture(texture.id);

	// We calculate the normalized texture coordinates for the desired texture-source-rectangle
	// It means converting from (tex.width, tex.height) coordinates to [0.0f, 1.0f] equivalent 
	rlBegin(RL_QUADS);
	rlColor4ub(color.r, color.g, color.b, color.a);

	// Front face
	rlNormal3f(0.0f, 0.0f, 1.0f);
	rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x - width/2, y - height/2, z + length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x + width/2, y - height/2, z + length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
	rlVertex3f(x + width/2, y + height/2, z + length/2);
	rlTexCoord2f(source.x/texWidth, source.y/texHeight);
	rlVertex3f(x - width/2, y + height/2, z + length/2);

	// Back face
	rlNormal3f(0.0f, 0.0f, - 1.0f);
	rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x - width/2, y - height/2, z - length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
	rlVertex3f(x - width/2, y + height/2, z - length/2);
	rlTexCoord2f(source.x/texWidth, source.y/texHeight);
	rlVertex3f(x + width/2, y + height/2, z - length/2);
	rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x + width/2, y - height/2, z - length/2);

	// Top face
	rlNormal3f(0.0f, 1.0f, 0.0f);
	rlTexCoord2f(source.x/texWidth, source.y/texHeight);
	rlVertex3f(x - width/2, y + height/2, z - length/2);
	rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x - width/2, y + height/2, z + length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x + width/2, y + height/2, z + length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
	rlVertex3f(x + width/2, y + height/2, z - length/2);

	// Bottom face
	rlNormal3f(0.0f, - 1.0f, 0.0f);
	rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
	rlVertex3f(x - width/2, y - height/2, z - length/2);
	rlTexCoord2f(source.x/texWidth, source.y/texHeight);
	rlVertex3f(x + width/2, y - height/2, z - length/2);
	rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x + width/2, y - height/2, z + length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x - width/2, y - height/2, z + length/2);

	// Right face
	rlNormal3f(1.0f, 0.0f, 0.0f);
	rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x + width/2, y - height/2, z - length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
	rlVertex3f(x + width/2, y + height/2, z - length/2);
	rlTexCoord2f(source.x/texWidth, source.y/texHeight);
	rlVertex3f(x + width/2, y + height/2, z + length/2);
	rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x + width/2, y - height/2, z + length/2);

	// Left face
	rlNormal3f( - 1.0f, 0.0f, 0.0f);
	rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x - width/2, y - height/2, z - length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
	rlVertex3f(x - width/2, y - height/2, z + length/2);
	rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
	rlVertex3f(x - width/2, y + height/2, z + length/2);
	rlTexCoord2f(source.x/texWidth, source.y/texHeight);
	rlVertex3f(x - width/2, y + height/2, z - length/2);

	rlEnd();

	rlSetTexture(0);
}