150 lines
5.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - split screen
  4. *
  5. * Example originally created with raylib 3.7, last time updated with raylib 4.0
  6. *
  7. * Example contributed by Jeffery Myers (@JeffM2501) and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2021-2023 Jeffery Myers (@JeffM2501)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. Camera cameraPlayer1 = { 0 };
  17. Camera cameraPlayer2 = { 0 };
  18. // Scene drawing
  19. void DrawScene(void)
  20. {
  21. int count = 5;
  22. float spacing = 4;
  23. // Grid of cube trees on a plane to make a "world"
  24. DrawPlane((Vector3){ 0, 0, 0 }, (Vector2){ 50, 50 }, BEIGE); // Simple world plane
  25. for (float x = -count*spacing; x <= count*spacing; x += spacing)
  26. {
  27. for (float z = -count*spacing; z <= count*spacing; z += spacing)
  28. {
  29. DrawCube((Vector3) { x, 1.5f, z }, 1, 1, 1, LIME);
  30. DrawCube((Vector3) { x, 0.5f, z }, 0.25f, 1, 0.25f, BROWN);
  31. }
  32. }
  33. // Draw a cube at each player's position
  34. DrawCube(cameraPlayer1.position, 1, 1, 1, RED);
  35. DrawCube(cameraPlayer2.position, 1, 1, 1, BLUE);
  36. }
  37. //------------------------------------------------------------------------------------
  38. // Program main entry point
  39. //------------------------------------------------------------------------------------
  40. int main(void)
  41. {
  42. // Initialization
  43. //--------------------------------------------------------------------------------------
  44. const int screenWidth = 800;
  45. const int screenHeight = 450;
  46. InitWindow(screenWidth, screenHeight, "raylib [core] example - split screen");
  47. // Setup player 1 camera and screen
  48. cameraPlayer1.fovy = 45.0f;
  49. cameraPlayer1.up.y = 1.0f;
  50. cameraPlayer1.target.y = 1.0f;
  51. cameraPlayer1.position.z = -3.0f;
  52. cameraPlayer1.position.y = 1.0f;
  53. RenderTexture screenPlayer1 = LoadRenderTexture(screenWidth/2, screenHeight);
  54. // Setup player two camera and screen
  55. cameraPlayer2.fovy = 45.0f;
  56. cameraPlayer2.up.y = 1.0f;
  57. cameraPlayer2.target.y = 3.0f;
  58. cameraPlayer2.position.x = -3.0f;
  59. cameraPlayer2.position.y = 3.0f;
  60. RenderTexture screenPlayer2 = LoadRenderTexture(screenWidth / 2, screenHeight);
  61. // Build a flipped rectangle the size of the split view to use for drawing later
  62. Rectangle splitScreenRect = { 0.0f, 0.0f, (float)screenPlayer1.texture.width, (float)-screenPlayer1.texture.height };
  63. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  64. //--------------------------------------------------------------------------------------
  65. // Main game loop
  66. while (!WindowShouldClose()) // Detect window close button or ESC key
  67. {
  68. // Update
  69. //----------------------------------------------------------------------------------
  70. // If anyone moves this frame, how far will they move based on the time since the last frame
  71. // this moves thigns at 10 world units per second, regardless of the actual FPS
  72. float offsetThisFrame = 10.0f*GetFrameTime();
  73. // Move Player1 forward and backwards (no turning)
  74. if (IsKeyDown(KEY_W))
  75. {
  76. cameraPlayer1.position.z += offsetThisFrame;
  77. cameraPlayer1.target.z += offsetThisFrame;
  78. }
  79. else if (IsKeyDown(KEY_S))
  80. {
  81. cameraPlayer1.position.z -= offsetThisFrame;
  82. cameraPlayer1.target.z -= offsetThisFrame;
  83. }
  84. // Move Player2 forward and backwards (no turning)
  85. if (IsKeyDown(KEY_UP))
  86. {
  87. cameraPlayer2.position.x += offsetThisFrame;
  88. cameraPlayer2.target.x += offsetThisFrame;
  89. }
  90. else if (IsKeyDown(KEY_DOWN))
  91. {
  92. cameraPlayer2.position.x -= offsetThisFrame;
  93. cameraPlayer2.target.x -= offsetThisFrame;
  94. }
  95. //----------------------------------------------------------------------------------
  96. // Draw
  97. //----------------------------------------------------------------------------------
  98. // Draw Player1 view to the render texture
  99. BeginTextureMode(screenPlayer1);
  100. ClearBackground(SKYBLUE);
  101. BeginMode3D(cameraPlayer1);
  102. DrawScene();
  103. EndMode3D();
  104. DrawText("PLAYER1 W/S to move", 10, 10, 20, RED);
  105. EndTextureMode();
  106. // Draw Player2 view to the render texture
  107. BeginTextureMode(screenPlayer2);
  108. ClearBackground(SKYBLUE);
  109. BeginMode3D(cameraPlayer2);
  110. DrawScene();
  111. EndMode3D();
  112. DrawText("PLAYER2 UP/DOWN to move", 10, 10, 20, BLUE);
  113. EndTextureMode();
  114. // Draw both views render textures to the screen side by side
  115. BeginDrawing();
  116. ClearBackground(BLACK);
  117. DrawTextureRec(screenPlayer1.texture, splitScreenRect, (Vector2){ 0, 0 }, WHITE);
  118. DrawTextureRec(screenPlayer2.texture, splitScreenRect, (Vector2){ screenWidth/2.0f, 0 }, WHITE);
  119. EndDrawing();
  120. }
  121. // De-Initialization
  122. //--------------------------------------------------------------------------------------
  123. UnloadRenderTexture(screenPlayer1); // Unload render texture
  124. UnloadRenderTexture(screenPlayer2); // Unload render texture
  125. CloseWindow(); // Close window and OpenGL context
  126. //--------------------------------------------------------------------------------------
  127. return 0;
  128. }