I am using RayLib and I have this code simply to create a window:
void start_window(std::string windowName){
InitWindow(800, 450, windowName.c_str());
SetTargetFPS(60);
rlFPCamera cam;
cam.Setup(45,Vector3{ 1, 0, 0 });
cam.SetCameraPosition((Vector3){1.0f, 500.0f, 1.0f});
cam.MoveSpeed.z = 100;
cam.MoveSpeed.x = 100;
cam.FarPlane = 5000;
Material matDefault = LoadMaterialDefault();
Material matRed = LoadMaterialDefault();
Material matGreen = LoadMaterialDefault();
matDefault.maps[MATERIAL_MAP_DIFFUSE].color = BLUE;
matRed.maps[MATERIAL_MAP_DIFFUSE].color = RED;
matGreen.maps[MATERIAL_MAP_DIFFUSE].color = GREEN;
Model cube = LoadModelFromMesh(GenMeshCube(5,5,5));
int width = 3072;
int height = 3072;
// Dynamic memory allocation to store pixels data (Color type)
Color *pixels = (Color *)malloc(width*height*sizeof(Color));
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (((x/16+y/16)/1)%2 == 0) pixels[y*width + x] = WHITE;
else pixels[y*width + x] = BLUE;
}
}
Image checkedIm = {
.data = pixels, // We can assign pixels directly to data
.width = width,
.height = height,
.mipmaps = 1,
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
};
Texture2D checked = LoadTextureFromImage(checkedIm);
Model terrain = LoadModel("Resources/Terrains/TestingGround.obj");
terrain.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = checked;
Vector3 cubePosition = {2.50f, 3.0f, 2.50f};
UnloadImage(checkedIm);
while (!WindowShouldClose()) // Detect window close button or ESC key
{
cam.Update();
float offsetThisFrame = 100.0f*GetFrameTime();
BeginDrawing();
ClearBackground(SKYBLUE);
cam.BeginMode3D();
DrawGrid(1000, 1.0f);
if (IsKeyDown(KEY_SPACE))
{
cam.SetCameraPosition((Vector3){cam.GetCameraPosition().x, cam.GetCameraPosition().y+offsetThisFrame, cam.GetCameraPosition().z});
}
else if (IsKeyDown(KEY_Z))
{
cam.SetCameraPosition((Vector3){cam.GetCameraPosition().x, cam.GetCameraPosition().y-offsetThisFrame, cam.GetCameraPosition().z});
}
if (IsKeyPressed(KEY_SPACE))
{
cubePosition = {cubePosition.x, cubePosition.y + 5, cubePosition.z};
}
VibraniumEngine::DrawModel(cube, (Vector3){50.30f, 12.32f, 40.7f}, 1, RED);
VibraniumEngine::DrawModel(cube, (Vector3){68.8f, 8.8f, 74.7f}, 1, RED);
VibraniumEngine::DrawModel(cube, (Vector3){15.52f, 11.65f, 84.51f}, 1, RED);
VibraniumEngine::DrawModel(cube, cubePosition, 1, BLUE);
DrawModelEx(terrain, (Vector3){0.0f, 0.0f, 0.0f},(Vector3){0.0f, 0.0f, 0.0f}, 0.0f,(Vector3){1.0f, 1.0f, 1.0f}, WHITE);
EndMode3D();
DrawFPS( 10, 10);
DrawText("Press H to hide the Map Window.", 10, 32, 12, GREEN);
DrawText(TextFormat("Camera Pos: X: %3.2fY: %3.2f Z: %3.2f",
cam.GetCameraPosition().x * -1,
cam.GetCameraPosition().y,
cam.GetCameraPosition().z), 10, 52, 12, BLACK);
EndDrawing();
}
UnloadModel(cube);
CloseWindow();
}
The code above executes and runs well.
If I try to start the above code with only one thread like this for example:
std::thread game1 (&start_window, "test");
It works perfectly. 1 Windows is created. So far, so good. However, if I try to do:
std::thread game1 (&start_window, "test");
std::thread game2 (&start_window, "test 2");
This does not work. No windows start at all. Any idea why and is it even possible to start two RayLib windows from two threads, if so how can I do it?