Reviewed example: core_split_screen
This commit is contained in:
parent
deaa84d28d
commit
2370af598e
1 changed files with 81 additions and 87 deletions
|
@ -2,53 +2,42 @@
|
||||||
*
|
*
|
||||||
* raylib [core] example - split screen
|
* raylib [core] example - split screen
|
||||||
*
|
*
|
||||||
* Welcome to raylib!
|
|
||||||
*
|
|
||||||
* To test examples, just press F6 and execute raylib_compile_execute script
|
|
||||||
* Note that compiled executable is placed in the same folder as .c file
|
|
||||||
*
|
|
||||||
* You can find all basic examples on C:\raylib\raylib\examples folder or
|
|
||||||
* raylib official webpage: www.raylib.com
|
|
||||||
*
|
|
||||||
* Enjoy using raylib. :)
|
|
||||||
*
|
|
||||||
* This example has been created using raylib 3.7 (www.raylib.com)
|
* This example has been created using raylib 3.7 (www.raylib.com)
|
||||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
|
* Example contributed by Jeffery Myers (@JeffM2501) and reviewed by Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Jeffery Myers (@JeffM2501)
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
Texture2D GridTexture;
|
Texture2D textureGrid = { 0 };
|
||||||
Camera Player1Camera = { 0 };
|
Camera cameraPlayer1 = { 0 };
|
||||||
Camera Player2Camera = { 0 };
|
Camera cameraPlayer2 = { 0 };
|
||||||
|
|
||||||
void DrawScene()
|
// Scene drawing
|
||||||
|
void DrawScene(void)
|
||||||
{
|
{
|
||||||
// grid of cube trees on a plane to make a "world"
|
|
||||||
DrawPlane((Vector3){ 0, 0, 0 }, (Vector2){ 50, 50 }, BEIGE); // simple world plane
|
|
||||||
float spacing = 4;
|
|
||||||
int count = 5;
|
int count = 5;
|
||||||
|
float spacing = 4;
|
||||||
|
|
||||||
|
// Grid of cube trees on a plane to make a "world"
|
||||||
|
DrawPlane((Vector3){ 0, 0, 0 }, (Vector2){ 50, 50 }, BEIGE); // Simple world plane
|
||||||
|
|
||||||
for (float x = -count * spacing; x <= count * spacing; x += spacing)
|
for (float x = -count*spacing; x <= count*spacing; x += spacing)
|
||||||
{
|
{
|
||||||
for (float z = -count * spacing; z <= count * spacing; z += spacing)
|
for (float z = -count*spacing; z <= count*spacing; z += spacing)
|
||||||
{
|
{
|
||||||
Vector3 pos = { x, 0.5f, z };
|
DrawCubeTexture(textureGrid, (Vector3) { x, 1.5f, z }, 1, 1, 1, GREEN);
|
||||||
|
DrawCubeTexture(textureGrid, (Vector3) { x, 0.5f, z }, 0.25f, 1, 0.25f, BROWN);
|
||||||
Vector3 min = { x - 0.5f,0,z - 0.5f };
|
|
||||||
Vector3 max = { x + 0.5f,1,z + 0.5f };
|
|
||||||
|
|
||||||
DrawCubeTexture(GridTexture, (Vector3) { x, 1.5f, z }, 1, 1, 1, GREEN);
|
|
||||||
DrawCubeTexture(GridTexture, (Vector3) { x, 0.5f, z }, 0.25f, 1, 0.25f, BROWN);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw a cube at each player's position
|
// Draw a cube at each player's position
|
||||||
DrawCube(Player1Camera.position, 1, 1, 1, RED);
|
DrawCube(cameraPlayer1.position, 1, 1, 1, RED);
|
||||||
DrawCube(Player2Camera.position, 1, 1, 1, BLUE);
|
DrawCube(cameraPlayer2.position, 1, 1, 1, BLUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
@ -59,101 +48,106 @@ int main(void)
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - split screen");
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - split screen");
|
||||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
||||||
|
|
||||||
// generate a simple texture to use for trees
|
// Generate a simple texture to use for trees
|
||||||
Image img = GenImageChecked(256, 256, 32, 32, DARKGRAY, WHITE);
|
Image img = GenImageChecked(256, 256, 32, 32, DARKGRAY, WHITE);
|
||||||
GridTexture = LoadTextureFromImage(img);
|
textureGrid = LoadTextureFromImage(img);
|
||||||
UnloadImage(img);
|
UnloadImage(img);
|
||||||
SetTextureFilter(GridTexture, TEXTURE_FILTER_ANISOTROPIC_16X);
|
SetTextureFilter(textureGrid, TEXTURE_FILTER_ANISOTROPIC_16X);
|
||||||
SetTextureWrap(GridTexture, TEXTURE_WRAP_CLAMP);
|
SetTextureWrap(textureGrid, TEXTURE_WRAP_CLAMP);
|
||||||
|
|
||||||
|
// Setup player 1 camera and screen
|
||||||
|
cameraPlayer1.fovy = 45.0f;
|
||||||
|
cameraPlayer1.up.y = 1.0f;
|
||||||
|
cameraPlayer1.target.y = 1.0f;
|
||||||
|
cameraPlayer1.position.z = -3.0f;
|
||||||
|
cameraPlayer1.position.y = 1.0f;
|
||||||
|
|
||||||
// setup player 1 camera and screen
|
RenderTexture screenPlayer1 = LoadRenderTexture(screenWidth/2, screenHeight);
|
||||||
Player1Camera.fovy = 45;
|
|
||||||
Player1Camera.up.y = 1;
|
|
||||||
Player1Camera.target.y = 1;
|
|
||||||
Player1Camera.position.z = -3;
|
|
||||||
Player1Camera.position.y = 1;
|
|
||||||
|
|
||||||
RenderTexture player1Screen = LoadRenderTexture(screenWidth / 2, screenHeight);
|
// Setup player two camera and screen
|
||||||
|
cameraPlayer2.fovy = 45.0f;
|
||||||
|
cameraPlayer2.up.y = 1.0f;
|
||||||
|
cameraPlayer2.target.y = 3.0f;
|
||||||
|
cameraPlayer2.position.x = -3.0f;
|
||||||
|
cameraPlayer2.position.y = 3.0f;
|
||||||
|
|
||||||
// setup player two camera and screen
|
RenderTexture screenPlayer2 = LoadRenderTexture(screenWidth / 2, screenHeight);
|
||||||
Player2Camera.fovy = 45;
|
|
||||||
Player2Camera.up.y = 1;
|
|
||||||
Player2Camera.target.y =3;
|
|
||||||
Player2Camera.position.x = -3;
|
|
||||||
Player2Camera.position.y = 3;
|
|
||||||
|
|
||||||
RenderTexture player2Screen = LoadRenderTexture(screenWidth / 2, screenHeight);
|
|
||||||
|
|
||||||
// build a flipped rectangle the size of the split view to use for drawing later
|
// Build a flipped rectangle the size of the split view to use for drawing later
|
||||||
Rectangle splitScreenRect = { 0,0, (float)player1Screen.texture.width, (float)-player1Screen.texture.height };
|
Rectangle splitScreenRect = { 0.0f, 0.0f, (float)screenPlayer1.texture.width, (float)-screenPlayer1.texture.height };
|
||||||
|
|
||||||
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Main game loop
|
// Main game loop
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
{
|
{
|
||||||
// if anyone moves this frame, how far will they move based on the time since the last frame
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// If anyone moves this frame, how far will they move based on the time since the last frame
|
||||||
// this moves thigns at 10 world units per second, regardless of the actual FPS
|
// this moves thigns at 10 world units per second, regardless of the actual FPS
|
||||||
float offsetThisFrame = 10 * GetFrameTime();
|
float offsetThisFrame = 10.0f*GetFrameTime();
|
||||||
|
|
||||||
|
// Move player 1 forward and backwards (no turning)
|
||||||
// move player 1 forward and backwards (no turning)
|
|
||||||
if (IsKeyDown(KEY_W))
|
if (IsKeyDown(KEY_W))
|
||||||
{
|
{
|
||||||
Player1Camera.position.z += offsetThisFrame;
|
cameraPlayer1.position.z += offsetThisFrame;
|
||||||
Player1Camera.target.z += offsetThisFrame;
|
cameraPlayer1.target.z += offsetThisFrame;
|
||||||
}
|
}
|
||||||
else if (IsKeyDown(KEY_S))
|
else if (IsKeyDown(KEY_S))
|
||||||
{
|
{
|
||||||
Player1Camera.position.z -= offsetThisFrame;
|
cameraPlayer1.position.z -= offsetThisFrame;
|
||||||
Player1Camera.target.z -= offsetThisFrame;
|
cameraPlayer1.target.z -= offsetThisFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
// move player 2 forward and backwards (no turning)
|
// Move player 2 forward and backwards (no turning)
|
||||||
if (IsKeyDown(KEY_UP))
|
if (IsKeyDown(KEY_UP))
|
||||||
{
|
{
|
||||||
Player2Camera.position.x += offsetThisFrame;
|
cameraPlayer2.position.x += offsetThisFrame;
|
||||||
Player2Camera.target.x += offsetThisFrame;
|
cameraPlayer2.target.x += offsetThisFrame;
|
||||||
}
|
}
|
||||||
else if (IsKeyDown(KEY_DOWN))
|
else if (IsKeyDown(KEY_DOWN))
|
||||||
{
|
{
|
||||||
Player2Camera.position.x -= offsetThisFrame;
|
cameraPlayer2.position.x -= offsetThisFrame;
|
||||||
Player2Camera.target.x -= offsetThisFrame;
|
cameraPlayer2.target.x -= offsetThisFrame;
|
||||||
}
|
}
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
// draw player 1's view to the render texture
|
// Draw
|
||||||
BeginTextureMode(player1Screen);
|
//----------------------------------------------------------------------------------
|
||||||
ClearBackground(SKYBLUE);
|
// Draw player 1's view to the render texture
|
||||||
BeginMode3D(Player1Camera);
|
BeginTextureMode(screenPlayer1);
|
||||||
DrawScene();
|
ClearBackground(SKYBLUE);
|
||||||
EndMode3D();
|
BeginMode3D(cameraPlayer1);
|
||||||
DrawText("PLAYER1 W/S to move", 0, 0, 20, RED);
|
DrawScene();
|
||||||
|
EndMode3D();
|
||||||
|
DrawText("PLAYER1 W/S to move", 0, 0, 20, RED);
|
||||||
EndTextureMode();
|
EndTextureMode();
|
||||||
|
|
||||||
// draw player 2's view to the render texture
|
// Draw player 2's view to the render texture
|
||||||
BeginTextureMode(player2Screen);
|
BeginTextureMode(screenPlayer2);
|
||||||
ClearBackground(SKYBLUE);
|
ClearBackground(SKYBLUE);
|
||||||
BeginMode3D(Player2Camera);
|
BeginMode3D(cameraPlayer2);
|
||||||
DrawScene();
|
DrawScene();
|
||||||
EndMode3D();
|
EndMode3D();
|
||||||
DrawText("PLAYER2 UP/DOWN to move", 0, 0, 20, BLUE);
|
DrawText("PLAYER2 UP/DOWN to move", 0, 0, 20, BLUE);
|
||||||
EndTextureMode();
|
EndTextureMode();
|
||||||
|
|
||||||
// draw both view render textures to the screen side by side
|
// Draw both view render textures to the screen side by side
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
DrawTextureRec(player1Screen.texture, splitScreenRect, (Vector2) { 0, 0 }, WHITE);
|
DrawTextureRec(screenPlayer1.texture, splitScreenRect, (Vector2) { 0, 0 }, WHITE);
|
||||||
DrawTextureRec(player2Screen.texture, splitScreenRect, (Vector2) { screenWidth/2.0f, 0 }, WHITE);
|
DrawTextureRec(screenPlayer2.texture, splitScreenRect, (Vector2) { screenWidth/2.0f, 0 }, WHITE);
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
UnloadRenderTexture(player1Screen);
|
|
||||||
UnloadRenderTexture(player2Screen);
|
|
||||||
UnloadTexture(GridTexture);
|
|
||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
UnloadRenderTexture(screenPlayer1);
|
||||||
|
UnloadRenderTexture(screenPlayer2);
|
||||||
|
UnloadTexture(textureGrid);
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue