You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.7 KiB

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