diff options
| author | raysan5 <[email protected]> | 2017-04-17 16:42:01 +0200 |
|---|---|---|
| committer | raysan5 <[email protected]> | 2017-04-17 16:42:01 +0200 |
| commit | 881f134f4d2fb4419d50382284e19b4f8ca4660e (patch) | |
| tree | 065658f8b462dd76837f849450bdd3895134121a /docs/examples/src/textures | |
| parent | 3e082f1d6251e366d7be6019d0950ea7a9e6b5b4 (diff) | |
| download | raylib-881f134f4d2fb4419d50382284e19b4f8ca4660e.tar.gz raylib-881f134f4d2fb4419d50382284e19b4f8ca4660e.zip | |
Review and recompile web examples
Diffstat (limited to 'docs/examples/src/textures')
| -rw-r--r-- | docs/examples/src/textures/textures_particles_blending.c | 135 | ||||
| -rw-r--r-- | docs/examples/src/textures/textures_raw_data.c | 24 | ||||
| -rw-r--r-- | docs/examples/src/textures/textures_rectangle.c | 55 | ||||
| -rw-r--r-- | docs/examples/src/textures/textures_srcrec_dstrec.c | 12 |
4 files changed, 193 insertions, 33 deletions
diff --git a/docs/examples/src/textures/textures_particles_blending.c b/docs/examples/src/textures/textures_particles_blending.c new file mode 100644 index 00000000..842ac77d --- /dev/null +++ b/docs/examples/src/textures/textures_particles_blending.c @@ -0,0 +1,135 @@ +/******************************************************************************************* +* +* raylib example - particles blending +* +* This example has been created using raylib 1.7 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2017 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#define MAX_PARTICLES 200 + +// Particle structure with basic data +typedef struct { + Vector2 position; + Color color; + float alpha; + float size; + float rotation; + bool active; // NOTE: Use it to activate/deactive particle +} Particle; + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [textures] example - particles blending"); + + // Particles pool, reuse them! + Particle mouseTail[MAX_PARTICLES]; + + // 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 = 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, 0, smoke.width, smoke.height }, + (Rectangle){ mouseTail[i].position.x, mouseTail[i].position.y, smoke.width*mouseTail[i].size, smoke.height*mouseTail[i].size }, + (Vector2){ smoke.width*mouseTail[i].size/2, smoke.height*mouseTail[i].size/2 }, 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; +}
\ No newline at end of file diff --git a/docs/examples/src/textures/textures_raw_data.c b/docs/examples/src/textures/textures_raw_data.c index d1922180..b038792b 100644 --- a/docs/examples/src/textures/textures_raw_data.c +++ b/docs/examples/src/textures/textures_raw_data.c @@ -27,9 +27,9 @@ int main() // 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 + Image fudesumiRaw = LoadImageRaw("resources/fudesumi.raw", 384, 512, UNCOMPRESSED_R8G8B8A8, 0); + Texture2D fudesumi = LoadTextureFromImage(fudesumiRaw); // Upload CPU (RAM) image to GPU (VRAM) + UnloadImage(fudesumiRaw); // Unload CPU (RAM) image data // Generate a checked texture by code (1024x1024 pixels) int width = 1024; @@ -42,8 +42,8 @@ int main() { 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; + if (((x/32+y/32)/1)%2 == 0) pixels[y*height + x] = ORANGE; + else pixels[y*height + x] = GOLD; } } @@ -70,12 +70,14 @@ int main() ClearBackground(RAYWHITE); - DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.3f)); - DrawTexture(sonic, 330, -20, WHITE); + DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.5f)); + DrawTexture(fudesumi, 430, -30, 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); + DrawText("CHECKED TEXTURE ", 84, 100, 30, BROWN); + DrawText("GENERATED by CODE", 72, 164, 30, BROWN); + DrawText("and RAW IMAGE LOADING", 46, 226, 30, BROWN); + + DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, BROWN); EndDrawing(); //---------------------------------------------------------------------------------- @@ -83,7 +85,7 @@ int main() // De-Initialization //-------------------------------------------------------------------------------------- - UnloadTexture(sonic); // Texture unloading + UnloadTexture(fudesumi); // Texture unloading UnloadTexture(checked); // Texture unloading CloseWindow(); // Close window and OpenGL context diff --git a/docs/examples/src/textures/textures_rectangle.c b/docs/examples/src/textures/textures_rectangle.c index cca5b216..c90db8ac 100644 --- a/docs/examples/src/textures/textures_rectangle.c +++ b/docs/examples/src/textures/textures_rectangle.c @@ -11,6 +11,9 @@ #include "raylib.h" +#define MAX_FRAME_SPEED 15 +#define MIN_FRAME_SPEED 1 + int main() { // Initialization @@ -21,11 +24,16 @@ int main() 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 + Texture2D scarfy = LoadTexture("resources/scarfy.png"); // Texture loading - Vector2 position = { 350.0f, 240.0f }; - Rectangle frameRec = { 0, 0, guybrush.width/7, guybrush.height }; + Vector2 position = { 350.0f, 280.0f }; + Rectangle frameRec = { 0, 0, scarfy.width/6, scarfy.height }; int currentFrame = 0; + + int framesCounter = 0; + int framesSpeed = 8; // Number of spritesheet frames shown by second + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop @@ -33,14 +41,23 @@ int main() { // Update //---------------------------------------------------------------------------------- - if (IsKeyPressed(KEY_RIGHT)) + framesCounter++; + + if (framesCounter >= (60/framesSpeed)) { + framesCounter = 0; currentFrame++; - if (currentFrame > 6) currentFrame = 0; + if (currentFrame > 5) currentFrame = 0; - frameRec.x = currentFrame*guybrush.width/7; + frameRec.x = currentFrame*scarfy.width/6; } + + if (IsKeyPressed(KEY_RIGHT)) framesSpeed++; + else if (IsKeyPressed(KEY_LEFT)) framesSpeed--; + + if (framesSpeed > MAX_FRAME_SPEED) framesSpeed = MAX_FRAME_SPEED; + else if (framesSpeed < MIN_FRAME_SPEED) framesSpeed = MIN_FRAME_SPEED; //---------------------------------------------------------------------------------- // Draw @@ -49,19 +66,23 @@ int main() 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 + DrawTexture(scarfy, 15, 40, WHITE); + DrawRectangleLines(15, 40, scarfy.width, scarfy.height, LIME); + DrawRectangleLines(15 + frameRec.x, 40 + frameRec.y, frameRec.width, frameRec.height, RED); - DrawRectangleLines(35 + frameRec.x, 40 + frameRec.y, frameRec.width, frameRec.height, RED); + DrawText("FRAME SPEED: ", 165, 210, 10, DARKGRAY); + DrawText(FormatText("%02i FPS", framesSpeed), 575, 210, 10, DARKGRAY); + DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, DARKGRAY); - DrawText("PRESS RIGHT KEY to", 540, 310, 10, GRAY); - DrawText("CHANGE DRAWING RECTANGLE", 520, 330, 10, GRAY); + for (int i = 0; i < MAX_FRAME_SPEED; i++) + { + if (i < framesSpeed) DrawRectangle(250 + 21*i, 205, 20, 20, RED); + DrawRectangleLines(250 + 21*i, 205, 20, 20, MAROON); + } - 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); + DrawTextureRec(scarfy, frameRec, position, WHITE); // Draw part of the texture + + DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY); EndDrawing(); //---------------------------------------------------------------------------------- @@ -69,7 +90,7 @@ int main() // De-Initialization //-------------------------------------------------------------------------------------- - UnloadTexture(guybrush); // Texture unloading + UnloadTexture(scarfy); // Texture unloading CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- diff --git a/docs/examples/src/textures/textures_srcrec_dstrec.c b/docs/examples/src/textures/textures_srcrec_dstrec.c index 6d824ce6..53ffd1d0 100644 --- a/docs/examples/src/textures/textures_srcrec_dstrec.c +++ b/docs/examples/src/textures/textures_srcrec_dstrec.c @@ -21,10 +21,10 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [textures] examples - texture source and destination rectangles"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) - Texture2D guybrush = LoadTexture("resources/guybrush.png"); // Texture loading + Texture2D scarfy = LoadTexture("resources/scarfy.png"); // Texture loading - int frameWidth = guybrush.width/7; - int frameHeight = guybrush.height; + int frameWidth = scarfy.width/6; + int frameHeight = scarfy.height; // NOTE: Source rectangle (part of the texture to use for drawing) Rectangle sourceRec = { 0, 0, frameWidth, frameHeight }; @@ -59,10 +59,12 @@ int main() // destRec defines the rectangle where our texture part will fit (scaling it to fit) // origin defines the point of the texture used as reference for rotation and scaling // rotation defines the texture rotation (using origin as rotation point) - DrawTexturePro(guybrush, sourceRec, destRec, origin, rotation, WHITE); + DrawTexturePro(scarfy, sourceRec, destRec, origin, rotation, WHITE); DrawLine(destRec.x, 0, destRec.x, screenHeight, GRAY); DrawLine(0, destRec.y, screenWidth, destRec.y, GRAY); + + DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY); EndDrawing(); //---------------------------------------------------------------------------------- @@ -70,7 +72,7 @@ int main() // De-Initialization //-------------------------------------------------------------------------------------- - UnloadTexture(guybrush); // Texture unloading + UnloadTexture(scarfy); // Texture unloading CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- |
