Created
June 3, 2021 15:19
-
-
Save JeffM2501/85e3c3fa4c5296227ab91dd7d2dec471 to your computer and use it in GitHub Desktop.
Simple splitscreen implemntation for raylib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************************* | |
* | |
* raylib [core] example - Basic window | |
* | |
* 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 1.0 (www.raylib.com) | |
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |
* | |
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5) | |
* | |
********************************************************************************************/ | |
#include "raylib.h" | |
Texture2D GridTexture; | |
void DrawScene() | |
{ | |
// 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; | |
for (float x = -count * spacing; x <= count * spacing; x += spacing) | |
{ | |
for (float z = -count * spacing; z <= count * spacing; z += spacing) | |
{ | |
Vector3 pos = { x, 0.5f, z }; | |
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); | |
} | |
} | |
DrawCube((Vector3) { 2, 1, 0 }, 1, 1, 1, RED); | |
DrawCube((Vector3) { 0, 1, 2 }, 1, 1, 1, BLUE); | |
} | |
int main(void) | |
{ | |
// Initialization | |
//-------------------------------------------------------------------------------------- | |
const int screenWidth = 800; | |
const int screenHeight = 450; | |
InitWindow(screenWidth, screenHeight, "raylib [core] example - split screen"); | |
Image img = GenImageChecked(256, 256, 32, 32, DARKGRAY, WHITE); | |
GridTexture = LoadTextureFromImage(img); | |
UnloadImage(img); | |
SetTextureFilter(GridTexture, TEXTURE_FILTER_ANISOTROPIC_16X); | |
SetTextureWrap(GridTexture, TEXTURE_WRAP_CLAMP); | |
Camera player1Camera = { 0 }; | |
player1Camera.fovy = 45; | |
player1Camera.up.y = 1; | |
player1Camera.target.y = 1; | |
player1Camera.position.z = -3; | |
player1Camera.position.y = 1; | |
Camera player2Camera = { 0 }; | |
player2Camera.fovy = 45; | |
player2Camera.up.y = 1; | |
player2Camera.target.y =3; | |
player2Camera.position.x = -3; | |
player2Camera.position.y = 3; | |
RenderTexture player1Screen = LoadRenderTexture(screenWidth / 2, screenHeight); | |
RenderTexture player2Screen = LoadRenderTexture(screenWidth / 2, screenHeight); | |
SetTargetFPS(60); // Set our game to run at 60 frames-per-second | |
Rectangle splitScreenRect = { 0,0,player1Screen.texture.width,-player1Screen.texture.height }; | |
// Main game loop | |
while (!WindowShouldClose()) // Detect window close button or ESC key | |
{ | |
// draw player 1's view | |
BeginTextureMode(player1Screen); | |
ClearBackground(SKYBLUE); | |
BeginMode3D(player1Camera); | |
DrawScene(); | |
EndMode3D(); | |
EndTextureMode(); | |
// draw player 2's view | |
BeginTextureMode(player2Screen); | |
ClearBackground(LIGHTGRAY); | |
BeginMode3D(player2Camera); | |
DrawScene(); | |
EndMode3D(); | |
EndTextureMode(); | |
// draw the textures to the screen | |
BeginDrawing(); | |
ClearBackground(BLACK); | |
DrawTextureRec(player1Screen.texture, splitScreenRect, (Vector2) { 0, 0 }, WHITE); | |
DrawTextureRec(player2Screen.texture, splitScreenRect, (Vector2) { screenWidth/2, 0 }, WHITE); | |
EndDrawing(); | |
} | |
UnloadRenderTexture(player1Screen); | |
UnloadRenderTexture(player2Screen); | |
UnloadTexture(GridTexture); | |
// De-Initialization | |
//-------------------------------------------------------------------------------------- | |
CloseWindow(); // Close window and OpenGL context | |
//-------------------------------------------------------------------------------------- | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment