core_split_screen.c 6.0 KB

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