int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [texture] example - texture rectangle"); const char textLine1[] = "Lena image is a standard test image which has been in use since 1973."; const char textLine2[] = "It comprises 512x512 pixels, and it is probably the most widely used"; const char textLine3[] = "test image for all sorts of image processing algorithms."; // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) Texture2D texture = LoadTexture("resources/lena.png"); // Texture loading Rectangle eyesRec = { 225, 240, 155, 50 }; // Part of the texture to draw Vector2 position = { 369, 241 }; //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawText("LENA", 220, 100, 20, PINK); DrawTexture(texture, screenWidth/2 - 256, 0, Fade(WHITE, 0.1f)); // Draw background image DrawTextureRec(texture, eyesRec, position, WHITE); // Draw eyes part of image DrawText(textLine1, 220, 140, 10, DARKGRAY); DrawText(textLine2, 220, 160, 10, DARKGRAY); DrawText(textLine3, 220, 180, 10, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(texture); // Texture unloading CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
//---------------------------------------------------------------------------------- // Main entry point //---------------------------------------------------------------------------------- int main(void) { // Initialization //--------------------------------------------------------- const char windowTitle[30] = "SKULLY ESCAPE [KING GAMEJAM]"; InitWindow(screenWidth, screenHeight, windowTitle); // Global data loading (assets that must be available in all screens, i.e. fonts) InitAudioDevice(); music = LoadMusicStream("resources/audio/come_play_with_me.ogg"); PlayMusicStream(music); font = LoadSpriteFont("resources/textures/alagard.png"); doors = LoadTexture("resources/textures/doors.png"); sndDoor = LoadSound("resources/audio/door.ogg"); sndScream = LoadSound("resources/audio/scream.ogg"); InitPlayer(); // Setup and Init first screen currentScreen = LOGO; InitLogoScreen(); #if defined(PLATFORM_WEB) emscripten_set_main_loop(UpdateDrawFrame, 0, 1); #else SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { UpdateDrawFrame(); } #endif // De-Initialization //-------------------------------------------------------------------------------------- // Unload all global loaded data (i.e. fonts) here! UnloadPlayer(); UnloadSpriteFont(font); UnloadTexture(doors); UnloadSound(sndDoor); UnloadSound(sndScream); UnloadMusicStream(music); CloseAudioDevice(); CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
// Gameplay Screen Unload logic void UnloadAisle02Screen(void) { // TODO: Unload GAMEPLAY screen variables here! UnloadTexture(background); UnloadMonster(lamp); UnloadMonster(chair); UnloadMonster(picture); UnloadMonster(arc); }
void TextureManager::UnloadAllTextures() { // start at the begginning of the texture map std::map<unsigned int, GLuint>::iterator i = m_texID.begin(); // Unload the textures untill the end of the texture map is found while (i != m_texID.end()) UnloadTexture(i->first); // clear the texture map m_texID.clear(); }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib test - texture pro"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) Texture2D texture = LoadTexture("resources/raylib_logo.png"); // Texture loading Vector2 position = { 200, 100 }; Rectangle sourceRec = { 128, 128, 128, 128 }; Rectangle destRec = { 128, 128, 128, 128 }; Vector2 origin = { 64, 64 }; // NOTE: origin is relative to destRec size //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); //DrawTextureEx(texture, position, 45, 1, MAROON); DrawTexturePro(texture, sourceRec, destRec, origin, 45, GREEN); DrawLine(destRec.x, 0, destRec.x, screenHeight, RED); DrawLine(0, destRec.y, screenWidth, destRec.y, RED); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(texture); // Texture unloading CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
/** * @brief テクスチャ情報構造体クリア * @param texInfo [IN] テクスチャ情報構造体ポインタ * @param unloadFlag [IN] アンロードするかどうか */ void nxTextureManager::ClearTextureInfo( nxTextureInfo *texInfo, bool unloadFlag) { nxTexture *pTex = texInfo->pTex; if (unloadFlag) { UnloadTexture(texInfo); } pTex->Finalize(); delete(pTex); }
// Unload SpriteFont from GPU memory void UnloadSpriteFont(SpriteFont spriteFont) { // NOTE: Make sure spriteFont is not default font (fallback) if (spriteFont.texture.id != defaultFont.texture.id) { UnloadTexture(spriteFont.texture); free(spriteFont.charValues); free(spriteFont.charRecs); free(spriteFont.charOffsets); free(spriteFont.charAdvanceX); TraceLog(DEBUG, "Unloaded sprite font data"); } }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [textures] example - image loading"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) Image image = LoadImage("resources/raylib_logo.png"); // Loaded in CPU memory (RAM) Texture2D texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM) UnloadImage(image); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM //--------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2, WHITE); DrawText("this IS a texture loaded from an image!", 300, 370, 10, GRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(texture); // Texture unloading CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib test - Image loading"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) Image img = LoadImage("resources/raylib_logo.png"); Texture2D texture = CreateTexture(img, false); UnloadImage(img); //--------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2, WHITE); DrawText("this IS a texture!", 360, 370, 10, GRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(texture); // Texture unloading CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
bool Sprite::LoadFromTexture( RageTextureID ID ) { LOG->Trace( "Sprite::LoadFromTexture( %s )", ID.filename.c_str() ); if( !m_pTexture || !(m_pTexture->GetID() == ID) ) { /* Load the texture if it's not already loaded. We still need * to do the rest, even if it's the same texture, since we need * to reset Sprite::m_size, etc. */ UnloadTexture(); m_pTexture = TEXTUREMAN->LoadTexture( ID ); ASSERT( m_pTexture->GetTextureWidth() >= 0 ); ASSERT( m_pTexture->GetTextureHeight() >= 0 ); } ASSERT( m_pTexture != NULL ); /* Hack: if we load "_blank", mark the actor hidden, so we can short-circuit * rendering later on. (This helps NoteField rendering.) */ if( !SetExtension(Basename(ID.filename), "").CompareNoCase("_blank") ) this->SetHidden( true ); // the size of the sprite is the size of the image before it was scaled Sprite::m_size.x = (float)m_pTexture->GetSourceFrameWidth(); Sprite::m_size.y = (float)m_pTexture->GetSourceFrameHeight(); // Assume the frames of this animation play in sequential order with 0.1 second delay. m_States.clear(); for( int i=0; i<m_pTexture->GetNumFrames(); i++ ) { State newState = { i, 0.1f }; m_States.push_back( newState ); } // apply clipping (if any) if( m_fRememberedClipWidth != -1 && m_fRememberedClipHeight != -1 ) ScaleToClipped( m_fRememberedClipWidth, m_fRememberedClipHeight ); return true; }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool TextureManager::UnloadTexture(const TexHandle textureHandle) { boost::optional<U32> bytesFreed; // Number of bytes freed. if(!m_elementsMap.empty()) { ElementMap::iterator elementIter(m_elementsMap.end()); // Iterator into element map. // Search for the texture by the public handle. for(ElementMap::iterator i = m_elementsMap.begin(), end = m_elementsMap.end(); (i != end); ++i) { if(((*i).second).m_id < textureHandle) { elementIter = i; } } // Unload the texture. bytesFreed = UnloadTexture(elementIter); } return (bytesFreed.is_initialized()); }
bool GLENGINE::LoadTexture(const char * filename, const char * name, bool mask) { GLuint texture = 0; uint8_t * pixels; std::string str(name); TEXTURE * tex; tex = textures[str]; if (tex) { if (tex->pixels) UnloadTexture(name); } std::cout << "Loading texture \"" << filename << "\"..."; bool result = false; if (mask) result = CreateTex(filename, &texture, &pixels, "mask.bmp"); else result = CreateTex(filename, &texture, &pixels, 0); if (!result) { std::cout << "Failed\n"; return false; } PrintErrorLine(__LINE__); tex = new TEXTURE; tex->pixels = pixels; tex->texID = texture; textures[str] = tex; std::cout << "OK\n"; return true; }
int main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 1280; const int screenHeight = 720; // Init window InitWindow(screenWidth, screenHeight, "Dr. Turtle & Mr. GAMERA"); // Load game resources: textures Texture2D sky = LoadTexture("resources/sky.png"); Texture2D mountains = LoadTexture("resources/mountains.png"); Texture2D sea = LoadTexture("resources/sea.png"); Texture2D title = LoadTexture("resources/title.png"); Texture2D turtle = LoadTexture("resources/turtle.png"); Texture2D gamera = LoadTexture("resources/gamera.png"); Texture2D shark = LoadTexture("resources/shark.png"); Texture2D orca = LoadTexture("resources/orca.png"); Texture2D swhale = LoadTexture("resources/swhale.png"); Texture2D fish = LoadTexture("resources/fish.png"); // Define scrolling variables int backScrolling = 0; int seaScrolling = 0; // Define current screen GameScreen currentScreen = TITLE; // Define player variables int playerRail = 1; Rectangle playerBounds = { 30 + 14, playerRail*120 + 90 + 14, 100, 100 }; bool gameraMode = false; // Define enemies variables Rectangle enemyBounds[MAX_ENEMIES]; int enemyRail[MAX_ENEMIES]; int enemyType[MAX_ENEMIES]; bool enemyActive[MAX_ENEMIES]; float enemySpeed = 10; // Init enemies variables for (int i = 0; i < MAX_ENEMIES; i++) { // Define enemy type (all same probability) enemyType[i] = GetRandomValue(0, 3); // Define enemy rail enemyRail[i] = GetRandomValue(0, 4); // Define enemy bounding box enemyBounds[i] = (Rectangle) { screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; enemyActive[i] = false; } // Define additional game variables int foodBar = 0; int framesCounter = 0; SetTargetFPS(60); // Setup game frames per second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- framesCounter++; // Game screens management switch (currentScreen) { case TITLE: { // Sea scrolling seaScrolling -= 2; if (seaScrolling <= -screenWidth) seaScrolling = 0; // Press enter to change to gameplay screen if (IsKeyPressed(KEY_ENTER)) { currentScreen = GAMEPLAY; framesCounter = 0; } } break; case GAMEPLAY: { // Background scrolling logic backScrolling--; if (backScrolling <= -screenWidth) backScrolling = 0; // Sea scrolling logic seaScrolling -= (enemySpeed - 2); if (seaScrolling <= -screenWidth) seaScrolling = 0; // Player movement logic if (IsKeyPressed(KEY_DOWN)) playerRail++; else if (IsKeyPressed(KEY_UP)) playerRail--; // Check player not out of rails if (playerRail > 4) playerRail = 4; else if (playerRail < 0) playerRail = 0; // Update player bounds playerBounds = (Rectangle) { 30 + 14, playerRail*120 + 90 + 14, 100, 100 }; // Enemies activation logic (every 40 frames) if (framesCounter > 40) { for (int i = 0; i < MAX_ENEMIES; i++) { if (enemyActive[i] == false) { enemyActive[i] = true; i = MAX_ENEMIES; } } framesCounter = 0; } // Enemies logic for (int i = 0; i < MAX_ENEMIES; i++) { if (enemyActive[i]) { enemyBounds[i].x -= enemySpeed; } // Check enemies out of screen if (enemyBounds[i].x <= 0 - 128) { enemyType[i] = GetRandomValue(0, 3); enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle) { screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; enemyActive[i] = false; } } // Enemies speed increase every frame if (!gameraMode) enemySpeed += 0.005; // Check collision player vs enemies for (int i = 0; i < MAX_ENEMIES; i++) { if (enemyActive[i]) { if (CheckCollisionRecs(playerBounds, enemyBounds[i])) { if (enemyType[i] < 3) // Bad enemies { if (gameraMode) { foodBar += 15; // After enemy deactivation, reset enemy parameters to be reused enemyType[i] = GetRandomValue(0, 3); enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle) { screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; enemyActive[i] = false; } else { // Player die logic currentScreen = ENDING; framesCounter = 0; } } else // Sweet fish { enemyActive[i] = false; enemyType[i] = GetRandomValue(0, 3); enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle) { screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; if (!gameraMode) foodBar += 80; else foodBar += 25; if (foodBar == 400) { gameraMode = true; } } } } } // Gamera mode logic if (gameraMode) { foodBar--; if (foodBar <= 0) { gameraMode = false; enemySpeed -= 2; if (enemySpeed < 10) enemySpeed = 10; } } } break; case ENDING: { // Press enter to play again if (IsKeyPressed(KEY_ENTER)) { currentScreen = GAMEPLAY; // Reset player playerRail = 1; playerBounds = (Rectangle) { 30 + 14, playerRail*120 + 90 + 14, 100, 100 }; gameraMode = false; // Reset enemies data for (int i = 0; i < MAX_ENEMIES; i++) { enemyType[i] = GetRandomValue(0, 3); enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle) { screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; enemyActive[i] = false; } enemySpeed = 10; // Reset game variables foodBar = 0; framesCounter = 0; } } break; default: break; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // Draw background (common to all screens) DrawTexture(sky, 0, 0, WHITE); DrawTexture(mountains, backScrolling, 0, WHITE); DrawTexture(mountains, screenWidth + backScrolling, 0, WHITE); if (!gameraMode) { DrawTexture(sea, seaScrolling, 0, BLUE); DrawTexture(sea, screenWidth + seaScrolling, 0, BLUE); } else { DrawTexture(sea, seaScrolling, 0, RED); DrawTexture(sea, screenWidth + seaScrolling, 0, RED); } switch (currentScreen) { case TITLE: { // Draw title DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE); // Draw blinking text if ((framesCounter/30) % 2) DrawText("PRESS ENTER", 480, 480, 40, BLACK); } break; case GAMEPLAY: { // Draw water lines for (int i = 0; i < 5; i++) DrawRectangle(0, i*120 + 120, screenWidth, 110, Fade(SKYBLUE, 0.1f)); // Draw player if (!gameraMode) DrawTexture(turtle, playerBounds.x - 14, playerBounds.y - 14, WHITE); else DrawTexture(gamera, playerBounds.x - 64, playerBounds.y - 64, WHITE); // Draw player bounding box //if (!gameraMode) DrawRectangleRec(playerBounds, Fade(GREEN, 0.4f)); //else DrawRectangleRec(playerBounds, Fade(ORANGE, 0.4f)); // Draw enemies for (int i = 0; i < MAX_ENEMIES; i++) { if (enemyActive[i]) { // Draw enemies /* switch(enemyType[i]) { case 0: DrawTexture(shark, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break; case 1: DrawTexture(orca, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break; case 2: DrawTexture(swhale, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break; case 3: DrawTexture(fish, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break; default: break; } */ // Draw enemies bounding boxes switch(enemyType[i]) { case 0: DrawRectangleRec(enemyBounds[i], RED); break; case 1: DrawRectangleRec(enemyBounds[i], RED); break; case 2: DrawRectangleRec(enemyBounds[i], RED); break; case 3: DrawRectangleRec(enemyBounds[i], GREEN); break; default: break; } } } // Draw gameplay interface // Draw food bar DrawRectangle(20, 20, 400, 40, Fade(GRAY, 0.4f)); DrawRectangle(20, 20, foodBar, 40, ORANGE); DrawRectangleLines(20, 20, 400, 40, BLACK); if (gameraMode) { DrawText("GAMERA MODE", 60, 22, 40, GRAY); } } break; case ENDING: { // Draw a transparent black rectangle that covers all screen DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f)); DrawText("GAME OVER", 300, 200, 100, MAROON); // Draw blinking text if ((framesCounter/30) % 2) DrawText("PRESS ENTER to REPLAY", 400, 420, 30, LIGHTGRAY); } break; default: break; } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- // Unload textures UnloadTexture(sky); UnloadTexture(mountains); UnloadTexture(sea); UnloadTexture(title); UnloadTexture(turtle); UnloadTexture(gamera); UnloadTexture(shark); UnloadTexture(orca); UnloadTexture(swhale); UnloadTexture(fish); CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main(void) { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [textures] example - particles blending"); // Particles pool, reuse them! Particle mouseTail[MAX_PARTICLES] = { 0 }; // Initialize particles for (int i = 0; i < MAX_PARTICLES; i++) { mouseTail[i].position = (Vector2){ 0, 0 }; mouseTail[i].color = (Color){ GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255 }; mouseTail[i].alpha = 1.0f; mouseTail[i].size = (float)GetRandomValue(1, 30)/20.0f; mouseTail[i].rotation = (float)GetRandomValue(0, 360); mouseTail[i].active = false; } float gravity = 3.0f; Texture2D smoke = LoadTexture("resources/smoke.png"); int blending = BLEND_ALPHA; SetTargetFPS(60); //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // Activate one particle every frame and Update active particles // NOTE: Particles initial position should be mouse position when activated // NOTE: Particles fall down with gravity and rotation... and disappear after 2 seconds (alpha = 0) // NOTE: When a particle disappears, active = false and it can be reused. for (int i = 0; i < MAX_PARTICLES; i++) { if (!mouseTail[i].active) { mouseTail[i].active = true; mouseTail[i].alpha = 1.0f; mouseTail[i].position = GetMousePosition(); i = MAX_PARTICLES; } } for (int i = 0; i < MAX_PARTICLES; i++) { if (mouseTail[i].active) { mouseTail[i].position.y += gravity; mouseTail[i].alpha -= 0.01f; if (mouseTail[i].alpha <= 0.0f) mouseTail[i].active = false; mouseTail[i].rotation += 5.0f; } } if (IsKeyPressed(KEY_SPACE)) { if (blending == BLEND_ALPHA) blending = BLEND_ADDITIVE; else blending = BLEND_ALPHA; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(DARKGRAY); BeginBlendMode(blending); // Draw active particles for (int i = 0; i < MAX_PARTICLES; i++) { if (mouseTail[i].active) DrawTexturePro(smoke, (Rectangle){ 0.0f, 0.0f, (float)smoke.width, (float)smoke.height }, (Rectangle){ mouseTail[i].position.x, mouseTail[i].position.y, smoke.width*mouseTail[i].size, smoke.height*mouseTail[i].size }, (Vector2){ (float)(smoke.width*mouseTail[i].size/2.0f), (float)(smoke.height*mouseTail[i].size/2.0f) }, mouseTail[i].rotation, Fade(mouseTail[i].color, mouseTail[i].alpha)); } EndBlendMode(); DrawText("PRESS SPACE to CHANGE BLENDING MODE", 180, 20, 20, BLACK); if (blending == BLEND_ALPHA) DrawText("ALPHA BLENDING", 290, screenHeight - 40, 20, BLACK); else DrawText("ADDITIVE BLENDING", 280, screenHeight - 40, 20, RAYWHITE); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(smoke); CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing"); // Define our custom camera to look into our 3d world Camera camera = {{ 18.0f, 16.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM) Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM) Model map = LoadHeightmap(image, (Vector3) { 16, 8, 16 }); // Load heightmap model with defined size map.material.texDiffuse = texture; // Set map diffuse texture Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Set model position (depends on model scaling!) UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); Begin3dMode(camera); // NOTE: Model is scaled to 1/4 of its original size (128x128 units) DrawModel(map, mapPosition, 1.0f, RED); DrawGrid(20, 1.0f); End3dMode(); DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE); DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(texture); // Unload texture UnloadModel(map); // Unload model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
Sprite::~Sprite() { UnloadTexture(); }
int main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 1280; const int screenHeight = 720; // Init window InitWindow(screenWidth, screenHeight, "Dr. Turtle & Mr. GAMERA"); // Load game resources: textures Texture2D sky = LoadTexture("resources/sky.png"); Texture2D mountains = LoadTexture("resources/mountains.png"); Texture2D sea = LoadTexture("resources/sea.png"); // Define scrolling variables int backScrolling = 0; int seaScrolling = 0; // Define current screen GameScreen currentScreen = TITLE; SetTargetFPS(60); // Setup game frames per second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // Game screens management switch (currentScreen) { case TITLE: { // Sea scrolling seaScrolling -= 2; if (seaScrolling <= -screenWidth) seaScrolling = 0; // Press enter to change to gameplay screen if (IsKeyPressed(KEY_ENTER)) { currentScreen = GAMEPLAY; } } break; case GAMEPLAY: { // Background scrolling logic backScrolling--; if (backScrolling <= -screenWidth) backScrolling = 0; // Sea scrolling logic seaScrolling -= 8; if (seaScrolling <= -screenWidth) seaScrolling = 0; // Press enter to change to ending screen if (IsKeyPressed(KEY_ENTER)) { currentScreen = ENDING; } } break; case ENDING: { // Press enter to change to title screen if (IsKeyPressed(KEY_ENTER)) { currentScreen = TITLE; } } break; default: break; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // Draw background (common to all screens) DrawTexture(sky, 0, 0, WHITE); DrawTexture(mountains, backScrolling, 0, WHITE); DrawTexture(mountains, screenWidth + backScrolling, 0, WHITE); DrawTexture(sea, seaScrolling, 0, BLUE); DrawTexture(sea, screenWidth + seaScrolling, 0, BLUE); switch (currentScreen) { case TITLE: { // Draw title screen DrawText("PRESS ENTER", 450, 420, 40, BLACK); } break; case GAMEPLAY: { // Draw gameplay screen DrawText("GAMEPLAY SCREEN", 20, 20, 40, MAROON); } break; case ENDING: { // Draw ending screen // Draw a transparent black rectangle that covers all screen DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f)); DrawText("ENDING SCREEN", 20, 20, 40, DARKBLUE); } break; default: break; } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- // Unload textures UnloadTexture(sky); UnloadTexture(mountains); UnloadTexture(sea); CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 1280; const int screenHeight = 720; // Init window ShowLogo(); InitWindow(screenWidth, screenHeight, "Dr. Turtle & Mr. GAMERA"); // Initialize audio device InitAudioDevice(); // Load game resources: textures Texture2D sky = LoadTexture("resources/sky.png"); Texture2D mountains = LoadTexture("resources/mountains.png"); Texture2D sea = LoadTexture("resources/sea.png"); Texture2D title = LoadTexture("resources/title.png"); Texture2D turtle = LoadTexture("resources/turtle.png"); Texture2D gamera = LoadTexture("resources/gamera.png"); Texture2D shark = LoadTexture("resources/shark.png"); Texture2D orca = LoadTexture("resources/orca.png"); Texture2D swhale = LoadTexture("resources/swhale.png"); Texture2D fish = LoadTexture("resources/fish.png"); Texture2D gframe = LoadTexture("resources/gframe.png"); // Load game resources: fonts SpriteFont font = LoadSpriteFont("resources/komika.png"); // Load game resources: sounds Sound eat = LoadSound("resources/eat.wav"); Sound die = LoadSound("resources/die.wav"); Sound growl = LoadSound("resources/gamera.wav"); // Start playing streaming music PlayMusicStream("resources/speeding.ogg"); // Define scrolling variables int backScrolling = 0; int seaScrolling = 0; // Define current screen GameScreen currentScreen = TITLE; // Define player variables int playerRail = 1; Rectangle playerBounds = { 30 + 14, playerRail*120 + 90 + 14, 100, 100 }; bool gameraMode = false; // Define enemies variables Rectangle enemyBounds[MAX_ENEMIES]; int enemyRail[MAX_ENEMIES]; int enemyType[MAX_ENEMIES]; bool enemyActive[MAX_ENEMIES]; float enemySpeed = 10; // Init enemies variables for (int i = 0; i < MAX_ENEMIES; i++) { // Define enemy type (all same probability) //enemyType[i] = GetRandomValue(0, 3); // Probability system for enemies type int enemyProb = GetRandomValue(0, 100); if (enemyProb < 30) enemyType[i] = 0; else if (enemyProb < 60) enemyType[i] = 1; else if (enemyProb < 90) enemyType[i] = 2; else enemyType[i] = 3; // define enemy rail enemyRail[i] = GetRandomValue(0, 4); // Make sure not two consecutive enemies in the same row if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; enemyActive[i] = false; } // Define additional game variables int score = 0; float distance = 0.0f; int hiscore = 0; float hidistance = 0.0f; int foodBar = 0; int framesCounter = 0; unsigned char blue = 200; float timeCounter = 0; SetTargetFPS(60); // Setup game frames per second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- framesCounter++; // Sea color tint effect blue = 210 + 25 * sin(timeCounter); timeCounter += 0.01; // Game screens management switch (currentScreen) { case TITLE: { // Sea scrolling seaScrolling -= 2; if (seaScrolling <= -screenWidth) seaScrolling = 0; // Press enter to change to gameplay screen if (IsKeyPressed(KEY_ENTER)) { currentScreen = GAMEPLAY; framesCounter = 0; } } break; case GAMEPLAY: { // Background scrolling logic backScrolling--; if (backScrolling <= -screenWidth) backScrolling = 0; // Sea scrolling logic seaScrolling -= (enemySpeed - 2); if (seaScrolling <= -screenWidth) seaScrolling = 0; // Player movement logic if (IsKeyPressed(KEY_DOWN)) playerRail++; else if (IsKeyPressed(KEY_UP)) playerRail--; // Check player not out of rails if (playerRail > 4) playerRail = 4; else if (playerRail < 0) playerRail = 0; // Update player bounds playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 }; // Enemies activation logic (every 40 frames) if (framesCounter > 40) { for (int i = 0; i < MAX_ENEMIES; i++) { if (enemyActive[i] == false) { enemyActive[i] = true; i = MAX_ENEMIES; } } framesCounter = 0; } // Enemies logic for (int i = 0; i < MAX_ENEMIES; i++) { if (enemyActive[i]) { enemyBounds[i].x -= enemySpeed; } // Check enemies out of screen if (enemyBounds[i].x <= 0 - 128) { enemyActive[i] = false; enemyType[i] = GetRandomValue(0, 3); enemyRail[i] = GetRandomValue(0, 4); // Make sure not two consecutive enemies in the same row if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; } } if (!gameraMode) enemySpeed += 0.005; // Check collision player vs enemies for (int i = 0; i < MAX_ENEMIES; i++) { if (enemyActive[i]) { if (CheckCollisionRecs(playerBounds, enemyBounds[i])) { if (enemyType[i] < 3) // Bad enemies { if (gameraMode) { if (enemyType[i] == 0) score += 50; else if (enemyType[i] == 1) score += 150; else if (enemyType[i] == 2) score += 300; foodBar += 15; enemyActive[i] = false; // After enemy deactivation, reset enemy parameters to be reused enemyType[i] = GetRandomValue(0, 3); enemyRail[i] = GetRandomValue(0, 4); // Make sure not two consecutive enemies in the same row if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; PlaySound(eat); } else { // Player die logic PlaySound(die); currentScreen = ENDING; framesCounter = 0; // Save hiscore and hidistance for next game if (score > hiscore) hiscore = score; if (distance > hidistance) hidistance = distance; } } else // Sweet fish { enemyActive[i] = false; enemyType[i] = GetRandomValue(0, 3); enemyRail[i] = GetRandomValue(0, 4); // Make sure not two consecutive enemies in the same row if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; if (!gameraMode) foodBar += 80; else foodBar += 25; score += 10; if (foodBar == 400) { gameraMode = true; PlaySound(growl); } PlaySound(eat); } } } } // Gamera mode logic if (gameraMode) { foodBar--; if (foodBar <= 0) { gameraMode = false; enemySpeed -= 2; if (enemySpeed < 10) enemySpeed = 10; } } // Update distance counter distance += 0.5f; } break; case ENDING: { // Press enter to play again if (IsKeyPressed(KEY_ENTER)) { currentScreen = GAMEPLAY; // Reset player playerRail = 1; playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 }; gameraMode = false; // Reset enemies data for (int i = 0; i < MAX_ENEMIES; i++) { int enemyProb = GetRandomValue(0, 100); if (enemyProb < 30) enemyType[i] = 0; else if (enemyProb < 60) enemyType[i] = 1; else if (enemyProb < 90) enemyType[i] = 2; else enemyType[i] = 3; //enemyType[i] = GetRandomValue(0, 3); enemyRail[i] = GetRandomValue(0, 4); // Make sure not two consecutive enemies in the same row if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4); enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 }; enemyActive[i] = false; } enemySpeed = 10; // Reset game variables score = 0; distance = 0.0; foodBar = 0; framesCounter = 0; } } break; default: break; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // Draw background (common to all screens) DrawTexture(sky, 0, 0, WHITE); DrawTexture(mountains, backScrolling, 0, WHITE); DrawTexture(mountains, screenWidth + backScrolling, 0, WHITE); if (!gameraMode) { DrawTexture(sea, seaScrolling, 0, (Color){ 16, 189, blue, 255}); DrawTexture(sea, screenWidth + seaScrolling, 0, (Color){ 16, 189, blue, 255}); } else { DrawTexture(sea, seaScrolling, 0, (Color){ 255, 113, 66, 255}); DrawTexture(sea, screenWidth + seaScrolling, 0, (Color){ 255, 113, 66, 255}); } switch (currentScreen) { case TITLE: { // Draw title DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE); // Draw blinking text if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, GetFontBaseSize(font), 0, WHITE); } break; case GAMEPLAY: { // Draw water lines for (int i = 0; i < 5; i++) DrawRectangle(0, i*120 + 120, screenWidth, 110, Fade(SKYBLUE, 0.1f)); // Draw player if (!gameraMode) DrawTexture(turtle, playerBounds.x - 14, playerBounds.y - 14, WHITE); else DrawTexture(gamera, playerBounds.x - 64, playerBounds.y - 64, WHITE); // Draw player bounding box //if (!gameraMode) DrawRectangleRec(playerBounds, Fade(GREEN, 0.4f)); //else DrawRectangleRec(playerBounds, Fade(ORANGE, 0.4f)); // Draw enemies for (int i = 0; i < MAX_ENEMIES; i++) { if (enemyActive[i]) { // Draw enemies switch(enemyType[i]) { case 0: DrawTexture(shark, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break; case 1: DrawTexture(orca, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break; case 2: DrawTexture(swhale, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break; case 3: DrawTexture(fish, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break; default: break; } // Draw enemies bounding boxes /* switch(enemyType[i]) { case 0: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break; case 1: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break; case 2: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break; case 3: DrawRectangleRec(enemyBounds[i], Fade(GREEN, 0.5f)); break; default: break; } */ } } // Draw gameplay interface DrawRectangle(20, 20, 400, 40, Fade(GRAY, 0.4f)); DrawRectangle(20, 20, foodBar, 40, ORANGE); DrawRectangleLines(20, 20, 400, 40, BLACK); DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, GetFontBaseSize(font), -2, ORANGE); DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, GetFontBaseSize(font), -2, ORANGE); if (gameraMode) { DrawText("GAMERA MODE", 60, 22, 40, GRAY); DrawTexture(gframe, 0, 0, Fade(WHITE, 0.5f)); } } break; case ENDING: { // Draw a transparent black rectangle that covers all screen DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f)); DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, GetFontBaseSize(font)*3, -2, MAROON); DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, GetFontBaseSize(font), -2, GOLD); DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, GetFontBaseSize(font), -2, GOLD); DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, GetFontBaseSize(font), -2, ORANGE); DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, GetFontBaseSize(font), -2, ORANGE); // Draw blinking text if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, GetFontBaseSize(font), -2, LIGHTGRAY); } break; default: break; } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- // Unload textures UnloadTexture(sky); UnloadTexture(mountains); UnloadTexture(sea); UnloadTexture(gframe); UnloadTexture(title); UnloadTexture(turtle); UnloadTexture(shark); UnloadTexture(orca); UnloadTexture(swhale); UnloadTexture(fish); UnloadTexture(gamera); // Unload font texture UnloadSpriteFont(font); // Unload sounds UnloadSound(eat); UnloadSound(die); UnloadSound(growl); StopMusicStream(); // Stop music CloseAudioDevice(); // Close audio device CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main(void) { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze"); // Define the camera to look into our 3d world Camera camera = { { 0.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; Image imMap = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM) Texture2D cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM) Mesh mesh = GenMeshCubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f }); Model model = LoadModelFromMesh(mesh); // NOTE: By default each cube is mapped to one part of texture atlas Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture // Get map image data to be used for collision detection Color *mapPixels = GetImageData(imMap); UnloadImage(imMap); // Unload image from RAM Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position Vector3 playerPosition = camera.position; // Set player position SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- Vector3 oldCamPos = camera.position; // Store old camera position UpdateCamera(&camera); // Update camera // Check player collision (we simplify to 2D collision detection) Vector2 playerPos = { camera.position.x, camera.position.z }; float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision) int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f); int playerCellY = (int)(playerPos.y - mapPosition.z + 0.5f); // Out-of-limits security check if (playerCellX < 0) playerCellX = 0; else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1; if (playerCellY < 0) playerCellY = 0; else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1; // Check map collisions using image data and player position // TODO: Improvement: Just check player surrounding cells for collision for (int y = 0; y < cubicmap.height; y++) { for (int x = 0; x < cubicmap.width; x++) { if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel (CheckCollisionCircleRec(playerPos, playerRadius, (Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f }))) { // Collision detected, reset camera position camera.position = oldCamPos; } } } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, mapPosition, 1.0f, WHITE); // Draw maze map //DrawCubeV(playerPosition, (Vector3){ 0.2f, 0.4f, 0.2f }, RED); // Draw player EndMode3D(); DrawTextureEx(cubicmap, (Vector2){ GetScreenWidth() - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE); DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN); // Draw player position radar DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- free(mapPixels); // Unload color array UnloadTexture(cubicmap); // Unload cubicmap texture UnloadTexture(texture); // Unload map texture UnloadModel(model); // Unload map model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader"); // Define the camera to look into our 3d world Camera camera = {{ 3.0, 3.0, 3.0 }, { 0.0, 1.5, 0.0 }, { 0.0, 1.0, 0.0 }}; Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture SetModelTexture(&dwarf, texture); // Bind texture to model Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position Shader shader = LoadShader("resources/shaders/base.vs", "resources/shaders/bloom.fs"); // Load postpro shader SetPostproShader(shader); // Set fullscreen postprocessing shader // Setup orbital camera SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode SetCameraPosition(camera.position); // Set internal camera position to match our camera position SetCameraTarget(camera.target); // Set internal camera target to match our camera target SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(&camera); // Update internal camera and our camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); Begin3dMode(camera); DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture DrawGrid(10.0, 1.0); // Draw a grid End3dMode(); DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, BLACK); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(texture); // Unload texture UnloadModel(dwarf); // Unload model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture formats loading"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) Texture2D sonic[NUM_TEXTURES]; sonic[PNG_R8G8B8A8] = LoadTexture("resources/texture_formats/sonic.png"); // Load UNCOMPRESSED PVR texture data sonic[PVR_GRAYSCALE] = LoadTexture("resources/texture_formats/sonic_GRAYSCALE.pvr"); sonic[PVR_GRAY_ALPHA] = LoadTexture("resources/texture_formats/sonic_L8A8.pvr"); sonic[PVR_R5G6B5] = LoadTexture("resources/texture_formats/sonic_R5G6B5.pvr"); sonic[PVR_R5G5B5A1] = LoadTexture("resources/texture_formats/sonic_R5G5B5A1.pvr"); sonic[PVR_R4G4B4A4] = LoadTexture("resources/texture_formats/sonic_R4G4B4A4.pvr"); // Load UNCOMPRESSED DDS texture data sonic[DDS_R5G6B5] = LoadTexture("resources/texture_formats/sonic_R5G6B5.dds"); sonic[DDS_R5G5B5A1] = LoadTexture("resources/texture_formats/sonic_A1R5G5B5.dds"); sonic[DDS_R4G4B4A4] = LoadTexture("resources/texture_formats/sonic_A4R4G4B4.dds"); sonic[DDS_R8G8B8A8] = LoadTexture("resources/texture_formats/sonic_A8R8G8B8.dds"); // Load COMPRESSED DXT DDS texture data (if supported) sonic[DDS_DXT1_RGB] = LoadTexture("resources/texture_formats/sonic_DXT1_RGB.dds"); sonic[DDS_DXT1_RGBA] = LoadTexture("resources/texture_formats/sonic_DXT1_RGBA.dds"); sonic[DDS_DXT3_RGBA] = LoadTexture("resources/texture_formats/sonic_DXT3_RGBA.dds"); sonic[DDS_DXT5_RGBA] = LoadTexture("resources/texture_formats/sonic_DXT5_RGBA.dds"); // Load COMPRESSED ETC texture data (if supported) sonic[PKM_ETC1_RGB] = LoadTexture("resources/texture_formats/sonic_ETC1_RGB.pkm"); sonic[PKM_ETC2_RGB] = LoadTexture("resources/texture_formats/sonic_ETC2_RGB.pkm"); sonic[PKM_ETC2_EAC_RGBA] = LoadTexture("resources/texture_formats/sonic_ETC2_EAC_RGBA.pkm"); sonic[KTX_ETC1_RGB] = LoadTexture("resources/texture_formats/sonic_ETC1_RGB.ktx"); sonic[KTX_ETC2_RGB] = LoadTexture("resources/texture_formats/sonic_ETC2_RGB.ktx"); sonic[KTX_ETC2_EAC_RGBA] = LoadTexture("resources/texture_formats/sonic_ETC2_EAC_RGBA.ktx"); // Load COMPRESSED ASTC texture data (if supported) sonic[ASTC_4x4_LDR] = LoadTexture("resources/texture_formats/sonic_ASTC_4x4_ldr.astc"); sonic[ASTC_8x8_LDR] = LoadTexture("resources/texture_formats/sonic_ASTC_8x8_ldr.astc"); // Load COMPRESSED PVR texture data (if supported) sonic[PVR_PVRT_RGB] = LoadTexture("resources/texture_formats/sonic_PVRT_RGB.pvr"); sonic[PVR_PVRT_RGBA] = LoadTexture("resources/texture_formats/sonic_PVRT_RGBA.pvr"); int selectedFormat = PNG_R8G8B8A8; Rectangle selectRecs[NUM_TEXTURES]; for (int i = 0; i < NUM_TEXTURES; i++) { if (i < NUM_TEXTURES/2) selectRecs[i] = (Rectangle){ 40, 30 + 32*i, 150, 30 }; else selectRecs[i] = (Rectangle){ 40 + 152, 30 + 32*(i - NUM_TEXTURES/2), 150, 30 }; } // Texture sizes in KB float textureSizes[NUM_TEXTURES] = { 512*512*32/8/1024, //PNG_R8G8B8A8 (32 bpp) 512*512*8/8/1024, //PVR_GRAYSCALE (8 bpp) 512*512*16/8/1024, //PVR_GRAY_ALPHA (16 bpp) 512*512*16/8/1024, //PVR_R5G6B5 (16 bpp) 512*512*16/8/1024, //PVR_R5G5B5A1 (16 bpp) 512*512*16/8/1024, //PVR_R4G4B4A4 (16 bpp) 512*512*16/8/1024, //DDS_R5G6B5 (16 bpp) 512*512*16/8/1024, //DDS_R5G5B5A1 (16 bpp) 512*512*16/8/1024, //DDS_R4G4B4A4 (16 bpp) 512*512*32/8/1024, //DDS_R8G8B8A8 (32 bpp) 512*512*4/8/1024, //DDS_DXT1_RGB (4 bpp) -Compressed- 512*512*4/8/1024, //DDS_DXT1_RGBA (4 bpp) -Compressed- 512*512*8/8/1024, //DDS_DXT3_RGBA (8 bpp) -Compressed- 512*512*8/8/1024, //DDS_DXT5_RGBA (8 bpp) -Compressed- 512*512*4/8/1024, //PKM_ETC1_RGB (4 bpp) -Compressed- 512*512*4/8/1024, //PKM_ETC2_RGB (4 bpp) -Compressed- 512*512*8/8/1024, //PKM_ETC2_EAC_RGBA (8 bpp) -Compressed- 512*512*4/8/1024, //KTX_ETC1_RGB (4 bpp) -Compressed- 512*512*4/8/1024, //KTX_ETC2_RGB (4 bpp) -Compressed- 512*512*8/8/1024, //KTX_ETC2_EAC_RGBA (8 bpp) -Compressed- 512*512*8/8/1024, //ASTC_4x4_LDR (8 bpp) -Compressed- 512*512*2/8/1024, //ASTC_8x8_LDR (2 bpp) -Compressed- 512*512*4/8/1024, //PVR_PVRT_RGB (4 bpp) -Compressed- 512*512*4/8/1024, //PVR_PVRT_RGBA (4 bpp) -Compressed- }; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //--------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- if (IsKeyPressed(KEY_DOWN)) { selectedFormat++; if (selectedFormat >= NUM_TEXTURES) selectedFormat = 0; } else if (IsKeyPressed(KEY_UP)) { selectedFormat--; if (selectedFormat < 0) selectedFormat = NUM_TEXTURES - 1; } else if (IsKeyPressed(KEY_RIGHT)) { if (selectedFormat < NUM_TEXTURES/2) selectedFormat += NUM_TEXTURES/2; } else if (IsKeyPressed(KEY_LEFT)) { if (selectedFormat >= NUM_TEXTURES/2) selectedFormat -= NUM_TEXTURES/2; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // Draw rectangles for (int i = 0; i < NUM_TEXTURES; i++) { if (i == selectedFormat) { DrawRectangleRec(selectRecs[i], SKYBLUE); DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, BLUE); DrawText(formatText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(formatText[i], 10)/2, selectRecs[i].y + 11, 10, DARKBLUE); } else { DrawRectangleRec(selectRecs[i], LIGHTGRAY); DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, GRAY); DrawText(formatText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(formatText[i], 10)/2, selectRecs[i].y + 11, 10, DARKGRAY); } } // Draw selected texture if (sonic[selectedFormat].id != 0) { DrawTexture(sonic[selectedFormat], 350, -10, WHITE); } else { DrawRectangleLines(488, 165, 200, 110, DARKGRAY); DrawText("FORMAT", 550, 180, 20, MAROON); DrawText("NOT SUPPORTED", 500, 210, 20, MAROON); DrawText("ON YOUR GPU", 520, 240, 20, MAROON); } DrawText("Select texture format (use cursor keys):", 40, 10, 10, DARKGRAY); DrawText("Required GPU memory size (VRAM):", 40, 427, 10, DARKGRAY); DrawText(FormatText("%4.0f KB", textureSizes[selectedFormat]), 240, 420, 20, DARKBLUE); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(sonic[i]); CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) Image image = LoadImage("resources/parrots.png"); // Loaded in CPU memory (RAM) ImageFormat(&image, UNCOMPRESSED_R8G8B8A8); // Format image to RGBA 32bit (required for texture update) <-- ISSUE Texture2D texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM) int currentProcess = NONE; bool textureReload = false; Rectangle selectRecs[NUM_PROCESSES]; for (int i = 0; i < NUM_PROCESSES; i++) selectRecs[i] = (Rectangle){ 40, 50 + 32*i, 150, 30 }; SetTargetFPS(60); //--------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- if (IsKeyPressed(KEY_DOWN)) { currentProcess++; if (currentProcess > 7) currentProcess = 0; textureReload = true; } else if (IsKeyPressed(KEY_UP)) { currentProcess--; if (currentProcess < 0) currentProcess = 7; textureReload = true; } if (textureReload) { UnloadImage(image); // Unload current image data image = LoadImage("resources/parrots.png"); // Re-load image data // NOTE: Image processing is a costly CPU process to be done every frame, // If image processing is required in a frame-basis, it should be done // with a texture and by shaders switch (currentProcess) { case COLOR_GRAYSCALE: ImageColorGrayscale(&image); break; case COLOR_TINT: ImageColorTint(&image, GREEN); break; case COLOR_INVERT: ImageColorInvert(&image); break; case COLOR_CONTRAST: ImageColorContrast(&image, -40); break; case COLOR_BRIGHTNESS: ImageColorBrightness(&image, -80); break; case FLIP_VERTICAL: ImageFlipVertical(&image); break; case FLIP_HORIZONTAL: ImageFlipHorizontal(&image); break; default: break; } Color *pixels = GetImageData(image); // Get pixel data from image (RGBA 32bit) UpdateTexture(texture, pixels); // Update texture with new image data free(pixels); // Unload pixels data from RAM textureReload = false; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY); // Draw rectangles for (int i = 0; i < NUM_PROCESSES; i++) { DrawRectangleRec(selectRecs[i], (i == currentProcess) ? SKYBLUE : LIGHTGRAY); DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, (i == currentProcess) ? BLUE : GRAY); DrawText(processText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(processText[i], 10)/2, selectRecs[i].y + 11, 10, (i == currentProcess) ? DARKBLUE : DARKGRAY); } DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE); DrawRectangleLines(screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, texture.width, texture.height, BLACK); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(texture); // Unload texture from VRAM UnloadImage(image); // Unload image from RAM CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
//---------------------------------------------------------------------------------- // Main entry point //---------------------------------------------------------------------------------- int main() { // Initialization //-------------------------------------------------------------------------------------- const char windowTitle[30] = "raylib functionality demo"; //SetupFlags(FLAG_FULLSCREEN_MODE); InitWindow(screenWidth, screenHeight, windowTitle); InitAudioDevice(); // Initialize audio device // TITLE screen variables Initialization fontAlagard = LoadSpriteFont("resources/fonts/alagard.rbmf"); // rBMF font loading fontPixelplay = LoadSpriteFont("resources/fonts/pixelplay.rbmf"); // rBMF font loading fontMecha = LoadSpriteFont("resources/fonts/mecha.rbmf"); // rBMF font loading fontSetback = LoadSpriteFont("resources/fonts/setback.rbmf"); // rBMF font loading fontRomulus = LoadSpriteFont("resources/fonts/romulus.rbmf"); // rBMF font loading pongBallPosition = (Vector2){ screenWidth/2, screenHeight/2 + 20 }; pongBallSpeed = (Vector2){ 6, 6 }; pongPlayerRec = (Rectangle){ 20, screenHeight/2 - 50 + 40, 20, 100 }; pongEnemyRec = (Rectangle){ screenWidth - 40, screenHeight/2 - 60, 20, 120 }; // LOGO screen variables Initialization logoPositionX = screenWidth/2 - 128; logoPositionY = screenHeight/2 - 128; // MODULES screen variables Initialization raylibWindow = LoadTexture("resources/raylib_window.png"); raylibWindow01 = LoadTexture("resources/raylib_window_01.png"); raylibWindow02 = LoadTexture("resources/raylib_window_02.png"); raylibWindow03 = LoadTexture("resources/raylib_window_03.png"); platforms = LoadTexture("resources/platforms.png"); raylibLogoB = LoadTexture("resources/raylib_logo128x128.png"); lena = LoadTexture("resources/lena.png"); mandrill = LoadTexture("resources/mandrill.png"); texAlagard = LoadTexture("resources/fonts/custom_alagard.png"); fontMechaC = LoadSpriteFont("resources/fonts/custom_mecha.png"); fontAlagardC = LoadSpriteFont("resources/fonts/custom_alagard.png"); fontJupiterC = LoadSpriteFont("resources/fonts/custom_jupiter_crash.png"); ballPosition = (Vector2){ 520 + 656/2, 220 + 399/2 }; camera = (Camera){{ 0.0, 12.0, 15.0 }, { 0.0, 3.0, 0.0 }, { 0.0, 1.0, 0.0 }}; catTexture = LoadTexture("resources/catsham.png"); // Load model texture cat = LoadModel("resources/cat.obj"); // Load OBJ model SetModelTexture(&cat, catTexture); fxWav = LoadSound("resources/audio/weird.wav"); // Load WAV audio file fxOgg = LoadSound("resources/audio/tanatana.ogg"); // Load OGG audio file for (int i = 0; i < MAX_BALLS; i++) { soundBallsPosition[i] = (Vector2){ 650 + 560/2 + GetRandomValue(-280, 280), 220 + 200 + GetRandomValue(-200, 200) }; soundBallsColor[i] = (Color){ GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255 }; soundBallsRadius[i] = GetRandomValue(2, 50); soundBallsAlpha[i] = 1.0f; soundBallsActive[i] = false; } // ENDING screen variables Initialization raylibLogoA = LoadTexture("resources/raylib_logo.png"); #ifndef PLATFORM_WEB SetTargetFPS(60); #endif #if defined(PLATFORM_WEB) emscripten_set_main_loop(UpdateDrawOneFrame, 0, 1); #else //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose() && !closeWindow) // Detect window close button or ESC key { UpdateDrawOneFrame(); } #endif // De-Initialization //-------------------------------------------------------------------------------------- // Unload all loaded data (textures, fonts, audio) UnloadSpriteFont(fontAlagard); // SpriteFont unloading UnloadSpriteFont(fontPixelplay); // SpriteFont unloading UnloadSpriteFont(fontMecha); // SpriteFont unloading UnloadSpriteFont(fontSetback); // SpriteFont unloading UnloadSpriteFont(fontRomulus); // SpriteFont unloading UnloadTexture(raylibWindow); UnloadTexture(raylibWindow01); UnloadTexture(raylibWindow02); UnloadTexture(raylibWindow03); UnloadTexture(platforms); UnloadTexture(raylibLogoA); UnloadTexture(raylibLogoB); UnloadTexture(lena); UnloadTexture(mandrill); UnloadTexture(texAlagard); UnloadSpriteFont(fontMechaC); UnloadSpriteFont(fontAlagardC); UnloadSpriteFont(fontJupiterC); UnloadTexture(catTexture); UnloadModel(cat); UnloadSound(fxWav); UnloadSound(fxOgg); CloseAudioDevice(); CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [texture] example - texture rectangle"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) Texture2D guybrush = LoadTexture("resources/guybrush.png"); // Texture loading Vector2 position = { 350.0f, 240.0f }; Rectangle frameRec = { 0, 0, guybrush.width/7, guybrush.height }; int currentFrame = 0; //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- if (IsKeyPressed(KEY_RIGHT)) { currentFrame++; if (currentFrame > 6) currentFrame = 0; frameRec.x = currentFrame*guybrush.width/7; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawTexture(guybrush, 35, 40, WHITE); DrawRectangleLines(35, 40, guybrush.width, guybrush.height, LIME); DrawTextureRec(guybrush, frameRec, position, WHITE); // Draw part of the texture DrawRectangleLines(35 + frameRec.x, 40 + frameRec.y, frameRec.width, frameRec.height, RED); DrawText("PRESS RIGHT KEY to", 540, 310, 10, GRAY); DrawText("CHANGE DRAWING RECTANGLE", 520, 330, 10, GRAY); DrawText("Guybrush Ulysses Threepwood,", 100, 300, 10, GRAY); DrawText("main character of the Monkey Island series", 80, 320, 10, GRAY); DrawText("of computer adventure games by LucasArts.", 80, 340, 10, GRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(guybrush); // Texture unloading CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main(void) { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib example - obj viewer"); // Define the camera to look into our 3d world Camera camera = { { 30.0f, 30.0f, 30.0f }, { 0.0f, 10.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; Model model = LoadModel("resources/models/turret.obj"); // Load default model obj Texture2D texture = LoadTexture("resources/models/turret_diffuse.png"); // Load default model texture model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Bind texture to model Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position BoundingBox bounds = MeshBoundingBox(model.meshes[0]); // Set model bounds bool selected = false; // Selected object flag SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode char objFilename[64] = "turret.obj"; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- if (IsFileDropped()) { int count = 0; char **droppedFiles = GetDroppedFiles(&count); if (count == 1) { if (IsFileExtension(droppedFiles[0], ".obj")) { for (int i = 0; i < model.meshCount; i++) UnloadMesh(&model.meshes[i]); model.meshes = LoadMeshes(droppedFiles[0], &model.meshCount); bounds = MeshBoundingBox(model.meshes[0]); } else if (IsFileExtension(droppedFiles[0], ".png")) { UnloadTexture(texture); texture = LoadTexture(droppedFiles[0]); model.materials[0].maps[MAP_DIFFUSE].texture = texture; } strcpy(objFilename, GetFileName(droppedFiles[0])); } ClearDroppedFiles(); // Clear internal buffers } UpdateCamera(&camera); // Select model on mouse click if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { // Check collision between ray and box if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) selected = !selected; else selected = false; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, position, 1.0f, WHITE); // Draw 3d model with texture DrawGrid(20.0, 10.0); // Draw a grid if (selected) DrawBoundingBox(bounds, GREEN); EndMode3D(); DrawText("Free camera default controls:", 10, 20, 10, DARKGRAY); DrawText("- Mouse Wheel to Zoom in-out", 20, 40, 10, GRAY); DrawText("- Mouse Wheel Pressed to Pan", 20, 60, 10, GRAY); DrawText("- Alt + Mouse Wheel Pressed to Rotate", 20, 80, 10, GRAY); DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 20, 100, 10, GRAY); DrawText("Drag & drop .obj/.png to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY); DrawText(FormatText("Current file: %s", objFilename), 250, GetScreenHeight() - 20, 10, GRAY); if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN); DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(model); // Unload model ClearDroppedFiles(); // Clear internal buffers CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "Floppy Bird"); InitAudioDevice(); // Initialize audio device Sound coin = LoadSound("resources/coin.wav"); Sound jump = LoadSound("resources/jump.wav"); Texture2D background = LoadTexture("resources/background.png"); Texture2D tubes = LoadTexture("resources/tubes.png"); Texture2D floppy = LoadTexture("resources/floppy.png"); Vector2 floppyPos = { 80, screenHeight/2 - floppy.height/2 }; Vector2 tubesPos[MAX_TUBES]; int tubesSpeedX = 2; for (int i = 0; i < MAX_TUBES; i++) { tubesPos[i].x = 400 + 280*i; tubesPos[i].y = -GetRandomValue(0, 120); } Rectangle tubesRecs[MAX_TUBES*2]; bool tubesActive[MAX_TUBES]; for (int i = 0; i < MAX_TUBES*2; i += 2) { tubesRecs[i].x = tubesPos[i/2].x; tubesRecs[i].y = tubesPos[i/2].y; tubesRecs[i].width = tubes.width; tubesRecs[i].height = 255; tubesRecs[i+1].x = tubesPos[i/2].x; tubesRecs[i+1].y = 600 + tubesPos[i/2].y - 255; tubesRecs[i+1].width = tubes.width; tubesRecs[i+1].height = 255; tubesActive[i/2] = true; } int backScroll = 0; int score = 0; int hiscore = 0; bool gameover = false; bool superfx = false; SetTargetFPS(60); //--------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- backScroll--; if (backScroll <= -800) backScroll = 0; for (int i = 0; i < MAX_TUBES; i++) tubesPos[i].x -= tubesSpeedX; for (int i = 0; i < MAX_TUBES*2; i += 2) { tubesRecs[i].x = tubesPos[i/2].x; tubesRecs[i+1].x = tubesPos[i/2].x; } if (IsKeyDown(KEY_SPACE) && !gameover) floppyPos.y -= 3; else floppyPos.y += 1; if (IsKeyPressed(KEY_SPACE) && !gameover) PlaySound(jump); // Check Collisions for (int i = 0; i < MAX_TUBES*2; i++) { if (CheckCollisionCircleRec((Vector2){ floppyPos.x + floppy.width/2, floppyPos.y + floppy.height/2 }, floppy.width/2, tubesRecs[i])) { gameover = true; } else if ((tubesPos[i/2].x < floppyPos.x) && tubesActive[i/2] && !gameover) { score += 100; tubesActive[i/2] = false; PlaySound(coin); superfx = true; if (score > hiscore) hiscore = score; } } if (gameover && IsKeyPressed(KEY_ENTER)) { for (int i = 0; i < MAX_TUBES; i++) { tubesPos[i].x = 400 + 280*i; tubesPos[i].y = -GetRandomValue(0, 120); } for (int i = 0; i < MAX_TUBES*2; i += 2) { tubesRecs[i].x = tubesPos[i/2].x; tubesRecs[i].y = tubesPos[i/2].y; tubesRecs[i+1].x = tubesPos[i/2].x; tubesRecs[i+1].y = 600 + tubesPos[i/2].y - 255; tubesActive[i/2] = true; } floppyPos.x = 80; floppyPos.y = screenHeight/2 - floppy.height/2; gameover = false; score = 0; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawTexture(background, backScroll, 0, WHITE); DrawTexture(background, screenWidth + backScroll, 0, WHITE); if (!gameover) { DrawTextureEx(floppy, floppyPos, 0, 1.0, WHITE); //DrawCircleLines(floppyPos.x + floppy.width/2, floppyPos.y + floppy.height/2, floppy.width/2, RED); } for (int i = 0; i < MAX_TUBES; i++) { if (tubesPos[i].x <= 800) DrawTextureEx(tubes, tubesPos[i], 0, 1.0, WHITE); //DrawRectangleLines(tubesRecs[i*2].x, tubesRecs[i*2].y, tubesRecs[i*2].width, tubesRecs[i*2].height, RED); //DrawRectangleLines(tubesRecs[i*2 + 1].x, tubesRecs[i*2 + 1].y, tubesRecs[i*2 + 1].width, tubesRecs[i*2 + 1].height, RED); } DrawText(FormatText("%04i", score), 20, 20, 40, PINK); DrawText(FormatText("HI-SCORE: %04i", hiscore), 20, 70, 20, VIOLET); if (gameover) { DrawText("GAME OVER", 100, 180, 100, MAROON); DrawText("PRESS ENTER to RETRY!", 280, 280, 20, RED); } if (superfx) { DrawRectangle(0, 0, screenWidth, screenHeight, GOLD); superfx = false; } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(background); // Texture unloading UnloadTexture(tubes); // Texture unloading UnloadTexture(floppy); // Texture unloading UnloadSound(coin); // Unload sound data UnloadSound(jump); // Unload sound data CloseAudioDevice(); // Close audio device CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [models] example - cubesmap loading and drawing"); // Define the camera to look into our 3d world Camera camera = {{ 16.0f, 14.0f, 16.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; Image image = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM) Texture2D cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM) Model map = LoadCubicmap(image); // Load cubicmap model (generate model from image) // NOTE: By default each cube is mapped to one part of texture atlas Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture map.material.texDiffuse = texture; // Set map diffuse texture Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); Begin3dMode(camera); DrawModel(map, mapPosition, 1.0f, WHITE); End3dMode(); DrawTextureEx(cubicmap, (Vector2){ screenWidth - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE); DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN); DrawText("cubicmap image used to", 658, 90, 10, GRAY); DrawText("generate map 3d model", 658, 104, 10, GRAY); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(cubicmap); // Unload cubicmap texture UnloadTexture(texture); // Unload map texture UnloadModel(map); // Unload map model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
//---------------------------------------------------------------------------------- // Main entry point //---------------------------------------------------------------------------------- int main(void) { // Initialization (Note windowTitle is unused on Android) //--------------------------------------------------------- InitWindow(screenWidth, screenHeight, "KOALA SEASONS"); // Load global data here (assets that must be available in all screens, i.e. fonts) font = LoadFont("resources/graphics/mainfont.png"); atlas01 = LoadTexture("resources/graphics/atlas01.png"); atlas02 = LoadTexture("resources/graphics/atlas02.png"); #if defined(PLATFORM_WEB) || defined(PLATFORM_RPI) || defined(PLATFORM_ANDROID) colorBlend = LoadShader("resources/shaders/glsl100/base.vs", "resources/shaders/glsl100/blend_color.fs"); #else colorBlend = LoadShader("resources/shaders/glsl330/base.vs", "resources/shaders/glsl330/blend_color.fs"); #endif InitAudioDevice(); // Load sounds data fxJump = LoadSound("resources/audio/jump.ogg"); fxDash = LoadSound("resources/audio/dash.ogg"); fxEatLeaves = LoadSound("resources/audio/eat_leaves.ogg"); fxHitResin = LoadSound("resources/audio/resin_hit.ogg"); fxWind = LoadSound("resources/audio/wind_sound.ogg"); fxDieSnake = LoadSound("resources/audio/snake_die.ogg"); fxDieDingo = LoadSound("resources/audio/dingo_die.ogg"); fxDieOwl = LoadSound("resources/audio/owl_die.ogg"); music = LoadMusicStream("resources/audio/jngl.xm"); PlayMusicStream(music); SetMusicVolume(music, 1.0f); // Define and init first screen // NOTE: currentScreen is defined in screens.h as a global variable currentScreen = TITLE; InitLogoScreen(); //InitOptionsScreen(); InitTitleScreen(); InitGameplayScreen(); InitEndingScreen(); #if defined(PLATFORM_WEB) emscripten_set_main_loop(UpdateDrawFrame, 0, 1); #else SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) UpdateDrawFrame(); #endif // De-Initialization //-------------------------------------------------------------------------------------- UnloadEndingScreen(); UnloadTitleScreen(); UnloadGameplayScreen(); UnloadLogoScreen(); UnloadTexture(atlas01); UnloadTexture(atlas02); UnloadFont(font); UnloadShader(colorBlend); // Unload color overlay blending shader UnloadSound(fxJump); UnloadSound(fxDash); UnloadSound(fxEatLeaves); UnloadSound(fxHitResin); UnloadSound(fxWind); UnloadSound(fxDieSnake); UnloadSound(fxDieDingo); UnloadSound(fxDieOwl); UnloadMusicStream(music); CloseAudioDevice(); // Close audio device CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from raw data"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) // Load RAW image data (512x512, 32bit RGBA, no file header) Image sonicRaw = LoadImageRaw("resources/texture_formats/sonic_R8G8B8A8.raw", 512, 512, UNCOMPRESSED_R8G8B8A8, 0); Texture2D sonic = LoadTextureFromImage(sonicRaw); // Upload CPU (RAM) image to GPU (VRAM) UnloadImage(sonicRaw); // Unload CPU (RAM) image data // Generate a checked texture by code (1024x1024 pixels) int width = 1024; int height = 1024; // 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/32+y/32)/1)%2 == 0) pixels[y*height + x] = DARKBLUE; else pixels[y*height + x] = SKYBLUE; } } // Load pixels data into an image structure and create texture Image checkedIm = LoadImageEx(pixels, width, height); Texture2D checked = LoadTextureFromImage(checkedIm); UnloadImage(checkedIm); // Unload CPU (RAM) image data // Dynamic memory must be freed after using it free(pixels); // Unload CPU (RAM) pixels data //--------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.3f)); DrawTexture(sonic, 330, -20, WHITE); DrawText("CHECKED TEXTURE ", 84, 100, 30, DARKBLUE); DrawText("GENERATED by CODE", 72, 164, 30, DARKBLUE); DrawText("and RAW IMAGE LOADING", 46, 226, 30, DARKBLUE); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(sonic); // Texture unloading UnloadTexture(checked); // Texture unloading CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }