core_split_screen.c 5.9 KB

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