Review ALL examples

This commit is contained in:
Ray 2019-05-20 16:36:42 +02:00
parent a43a7980a3
commit b525039e0a
96 changed files with 1301 additions and 1317 deletions

View file

@ -11,26 +11,26 @@
#include "raylib.h"
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling");
// NOTE: Be careful, background width must be equal or bigger than screen width
// if not, texture should be draw more than two times for scrolling effect
Texture2D background = LoadTexture("resources/cyberpunk_street_background.png");
Texture2D midground = LoadTexture("resources/cyberpunk_street_midground.png");
Texture2D foreground = LoadTexture("resources/cyberpunk_street_foreground.png");
float scrollingBack = 0;
float scrollingMid = 0;
float scrollingFore = 0;
SetTargetFPS(60);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
@ -41,7 +41,7 @@ int main()
scrollingBack -= 0.1f;
scrollingMid -= 0.5f;
scrollingFore -= 1.0f;
// NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
if (scrollingBack <= -background.width*2) scrollingBack = 0;
if (scrollingMid <= -midground.width*2) scrollingMid = 0;
@ -58,15 +58,15 @@ int main()
// NOTE: Texture is scaled twice its size
DrawTextureEx(background, (Vector2){ scrollingBack, 20 }, 0.0f, 2.0f, WHITE);
DrawTextureEx(background, (Vector2){ background.width*2 + scrollingBack, 20 }, 0.0f, 2.0f, WHITE);
// Draw midground image twice
DrawTextureEx(midground, (Vector2){ scrollingMid, 20 }, 0.0f, 2.0f, WHITE);
DrawTextureEx(midground, (Vector2){ midground.width*2 + scrollingMid, 20 }, 0.0f, 2.0f, WHITE);
// Draw foreground image twice
DrawTextureEx(foreground, (Vector2){ scrollingFore, 70 }, 0.0f, 2.0f, WHITE);
DrawTextureEx(foreground, (Vector2){ foreground.width*2 + scrollingFore, 70 }, 0.0f, 2.0f, WHITE);
DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, RED);
DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, RAYWHITE);

View file

@ -25,7 +25,7 @@ typedef struct Bunny {
Color color;
} Bunny;
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
@ -34,13 +34,14 @@ int main()
InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
// Load bunny texture
Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
int bunniesCount = 0; // Bunnies counter
int bunniesCount = 0; // Bunnies counter
SetTargetFPS(60);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
@ -56,8 +57,8 @@ int main()
bunnies[bunniesCount].position = GetMousePosition();
bunnies[bunniesCount].speed.x = (float)GetRandomValue(-250, 250)/60.0f;
bunnies[bunniesCount].speed.y = (float)GetRandomValue(-250, 250)/60.0f;
bunnies[bunniesCount].color = (Color){ GetRandomValue(50, 240),
GetRandomValue(80, 240),
bunnies[bunniesCount].color = (Color){ GetRandomValue(50, 240),
GetRandomValue(80, 240),
GetRandomValue(100, 240), 255 };
bunniesCount++;
}
@ -69,9 +70,9 @@ int main()
bunnies[i].position.x += bunnies[i].speed.x;
bunnies[i].position.y += bunnies[i].speed.y;
if (((bunnies[i].position.x + texBunny.width/2) > GetScreenWidth()) ||
if (((bunnies[i].position.x + texBunny.width/2) > GetScreenWidth()) ||
((bunnies[i].position.x + texBunny.width/2) < 0)) bunnies[i].speed.x *= -1;
if (((bunnies[i].position.y + texBunny.height/2) > GetScreenHeight()) ||
if (((bunnies[i].position.y + texBunny.height/2) > GetScreenHeight()) ||
((bunnies[i].position.y + texBunny.height/2 - 40) < 0)) bunnies[i].speed.y *= -1;
}
//----------------------------------------------------------------------------------
@ -84,9 +85,9 @@ int main()
for (int i = 0; i < bunniesCount; i++)
{
// NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
// NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
// a draw call is launched and buffer starts being filled again;
// before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
// before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
// Process of sending data is costly and it could happen that GPU data has not been completely
// processed for drawing while new data is tried to be sent (updating current in-use buffers)
// it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
@ -96,7 +97,7 @@ int main()
DrawRectangle(0, 0, screenWidth, 40, BLACK);
DrawText(FormatText("bunnies: %i", bunniesCount), 120, 10, 20, GREEN);
DrawText(FormatText("batched draw calls: %i", 1 + bunniesCount/MAX_BATCH_ELEMENTS), 320, 10, 20, MAROON);
DrawFPS(10, 10);
EndDrawing();
@ -106,7 +107,7 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
free(bunnies); // Unload bunnies data array
UnloadTexture(texBunny); // Unload bunny texture
CloseWindow(); // Close window and OpenGL context

View file

@ -13,12 +13,12 @@
#include "raylib.h"
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing");
@ -28,26 +28,26 @@ int main()
ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 }); // Crop an image piece
ImageFlipHorizontal(&cat); // Flip cropped image horizontally
ImageResize(&cat, 150, 200); // Resize flipped-cropped image
Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
// Draw one image over the other with a scaling of 1.5f
ImageDraw(&parrots, cat, (Rectangle){ 0, 0, cat.width, cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f });
ImageCrop(&parrots, (Rectangle){ 0, 50, parrots.width, parrots.height - 100 }); // Crop resulting image
UnloadImage(cat); // Unload image from RAM
// Load custom font for frawing on image
Font font = LoadFont("resources/custom_jupiter_crash.png");
// Draw over image using custom font
ImageDrawTextEx(&parrots, (Vector2){ 300, 230 }, font, "PARROTS & CAT", font.baseSize, -2, WHITE);
UnloadFont(font); // Unload custom spritefont (already drawn used on image)
Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
SetTargetFPS(60);
//---------------------------------------------------------------------------------------

View file

@ -13,12 +13,12 @@
#define NUM_TEXTURES 7 // Currently we have 7 generation algorithms
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - procedural images generation");
@ -38,7 +38,7 @@ int main()
textures[4] = LoadTextureFromImage(whiteNoise);
textures[5] = LoadTextureFromImage(perlinNoise);
textures[6] = LoadTextureFromImage(cellular);
// Unload image data (CPU RAM)
UnloadImage(verticalGradient);
UnloadImage(horizontalGradient);
@ -49,10 +49,10 @@ int main()
UnloadImage(cellular);
int currentTexture = 0;
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
@ -67,15 +67,15 @@ int main()
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTexture(textures[currentTexture], 0, 0, WHITE);
DrawRectangle(30, 400, 325, 30, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(30, 400, 325, 30, Fade(WHITE, 0.5f));
DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES", 40, 410, 10, WHITE);
switch(currentTexture)
{
case 0: DrawText("VERTICAL GRADIENT", 560, 10, 20, RAYWHITE); break;
@ -87,19 +87,19 @@ int main()
case 6: DrawText("CELLULAR", 670, 10, 20, RAYWHITE); break;
default: break;
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
// Unload textures data (GPU VRAM)
for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(textures[i]);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View file

@ -13,12 +13,12 @@
#include "raylib.h"
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image loading");

View file

@ -13,19 +13,19 @@
#include "raylib.h"
#include <stdlib.h> // Required for: free()
#include <stdlib.h> // Required for: free()
#define NUM_PROCESSES 8
typedef enum {
NONE = 0,
COLOR_GRAYSCALE,
COLOR_TINT,
COLOR_INVERT,
COLOR_CONTRAST,
COLOR_BRIGHTNESS,
FLIP_VERTICAL,
FLIP_HORIZONTAL
typedef enum {
NONE = 0,
COLOR_GRAYSCALE,
COLOR_TINT,
COLOR_INVERT,
COLOR_CONTRAST,
COLOR_BRIGHTNESS,
FLIP_VERTICAL,
FLIP_HORIZONTAL
} ImageProcess;
static const char *processText[] = {
@ -39,12 +39,12 @@ static const char *processText[] = {
"FLIP HORIZONTAL"
};
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing");
@ -58,9 +58,9 @@ int main()
bool textureReload = false;
Rectangle selectRecs[NUM_PROCESSES];
for (int i = 0; i < NUM_PROCESSES; i++) selectRecs[i] = (Rectangle){ 40.0f, (float)(50 + 32*i), 150.0f, 30.0f };
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
@ -81,14 +81,14 @@ int main()
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
// 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)
{
@ -101,11 +101,11 @@ int main()
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;
}
//----------------------------------------------------------------------------------
@ -115,9 +115,9 @@ int main()
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY);
// Draw rectangles
for (int i = 0; i < NUM_PROCESSES; i++)
{
@ -128,7 +128,7 @@ int main()
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();
//----------------------------------------------------------------------------------
}

View file

@ -11,28 +11,28 @@
#include "raylib.h"
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [texture] example - image text drawing");
Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
// TTF Font loading with custom generation parameters
Font font = LoadFontEx("resources/KAISG.ttf", 64, 0, 0);
Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
// Draw over image using custom font
ImageDrawTextEx(&parrots, (Vector2){ 20.0f, 20.0f }, font, "[Parrots font drawing]", (float)font.baseSize, 0.0f, RED);
Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
Vector2 position = { (float)(screenWidth/2 - texture.width/2), (float)(screenHeight/2 - texture.height/2 - 20) };
bool showFont = false;
SetTargetFPS(60);
@ -57,15 +57,15 @@ int main()
{
// Draw texture with text already drawn inside
DrawTextureV(texture, position, WHITE);
// Draw text directly using sprite font
DrawTextEx(font, "[Parrots font drawing]", (Vector2){ position.x + 20,
DrawTextEx(font, "[Parrots font drawing]", (Vector2){ position.x + 20,
position.y + 20 + 280 }, (float)font.baseSize, 0.0f, WHITE);
}
else DrawTexture(font.texture, screenWidth/2 - font.texture.width/2, 50, BLACK);
DrawText("PRESS SPACE to SEE USED SPRITEFONT ", 290, 420, 10, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
@ -73,9 +73,9 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture); // Texture unloading
UnloadFont(font); // Unload custom spritefont
UnloadFont(font); // Unload custom spritefont
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View file

@ -11,12 +11,12 @@
#include "raylib.h"
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture loading and drawing");

View file

@ -15,18 +15,18 @@
#include "raylib.h"
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - N-patch drawing");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D nPatchTexture = LoadTexture("resources/ninepatch_button.png");
Vector2 mousePosition = { 0 };
Vector2 origin = { 0.0f, 0.0f };
@ -39,13 +39,13 @@ int main()
// A 9-patch (NPT_9PATCH) changes its sizes in both axis
NPatchInfo ninePatchInfo1 = { (Rectangle){ 0.0f, 0.0f, 64.0f, 64.0f }, 12, 40, 12, 12, NPT_9PATCH };
NPatchInfo ninePatchInfo2 = { (Rectangle){ 0.0f, 128.0f, 64.0f, 64.0f }, 16, 16, 16, 16, NPT_9PATCH };
// A horizontal 3-patch (NPT_3PATCH_HORIZONTAL) changes its sizes along the x axis only
NPatchInfo h3PatchInfo = { (Rectangle){ 0.0f, 64.0f, 64.0f, 64.0f }, 8, 8, 8, 8, NPT_3PATCH_HORIZONTAL };
// A vertical 3-patch (NPT_3PATCH_VERTICAL) changes its sizes along the y axis only
NPatchInfo v3PatchInfo = { (Rectangle){ 0.0f, 192.0f, 64.0f, 64.0f }, 6, 6, 6, 6, NPT_3PATCH_VERTICAL };
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
@ -55,7 +55,7 @@ int main()
// Update
//----------------------------------------------------------------------------------
mousePosition = GetMousePosition();
// Resize the n-patches based on mouse position
dstRec1.width = mousePosition.x - dstRec1.x;
dstRec1.height = mousePosition.y - dstRec1.y;
@ -86,7 +86,7 @@ int main()
DrawTextureNPatch(nPatchTexture, ninePatchInfo1, dstRec1, origin, 0.0f, WHITE);
DrawTextureNPatch(nPatchTexture, h3PatchInfo, dstRecH, origin, 0.0f, WHITE);
DrawTextureNPatch(nPatchTexture, v3PatchInfo, dstRecV, origin, 0.0f, WHITE);
// Draw the source texture
DrawRectangleLines(5, 88, 74, 266, BLUE);
DrawTexture(nPatchTexture, 10, 93, WHITE);

View file

@ -23,18 +23,18 @@ typedef struct {
bool active; // NOTE: Use it to activate/deactive particle
} Particle;
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - particles blending");
// Particles pool, reuse them!
Particle mouseTail[MAX_PARTICLES];
Particle mouseTail[MAX_PARTICLES];
// Initialize particles
for (int i = 0; i < MAX_PARTICLES; i++)
{
@ -45,13 +45,13 @@ int main()
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);
//--------------------------------------------------------------------------------------
@ -60,7 +60,7 @@ int main()
{
// 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)
@ -82,13 +82,13 @@ int main()
{
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;
@ -101,25 +101,25 @@ int main()
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 },
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();
//----------------------------------------------------------------------------------
}
@ -127,7 +127,7 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(smoke);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View file

@ -13,31 +13,31 @@
#include "raylib.h"
#include <stdlib.h> // Required for malloc() and free()
#include <stdlib.h> // Required for: malloc() and free()
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const 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 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
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 = 960;
int height = 480;
// 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++)
@ -46,14 +46,14 @@ int main()
else pixels[y*width + x] = GOLD;
}
}
// 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
UnloadImage(checkedIm); // Unload CPU (RAM) image data
// Dynamic memory must be freed after using it
free(pixels); // Unload CPU (RAM) pixels data
free(pixels); // Unload CPU (RAM) pixels data
//---------------------------------------------------------------------------------------
// Main game loop
@ -76,7 +76,7 @@ int main()
DrawText("CHECKED TEXTURE ", 84, 85, 30, BROWN);
DrawText("GENERATED by CODE", 72, 148, 30, BROWN);
DrawText("and RAW IMAGE LOADING", 46, 210, 30, BROWN);
DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, BROWN);
EndDrawing();

View file

@ -14,12 +14,12 @@
#define MAX_FRAME_SPEED 15
#define MIN_FRAME_SPEED 1
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [texture] example - texture rectangle");
@ -29,10 +29,10 @@ int main()
Vector2 position = { 350.0f, 280.0f };
Rectangle frameRec = { 0.0f, 0.0f, (float)scarfy.width/6, (float)scarfy.height };
int currentFrame = 0;
int framesCounter = 0;
int framesSpeed = 8; // Number of spritesheet frames shown by second
int framesSpeed = 8; // Number of spritesheet frames shown by second
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -42,20 +42,20 @@ int main()
// Update
//----------------------------------------------------------------------------------
framesCounter++;
if (framesCounter >= (60/framesSpeed))
{
framesCounter = 0;
currentFrame++;
if (currentFrame > 5) currentFrame = 0;
frameRec.x = (float)currentFrame*(float)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;
//----------------------------------------------------------------------------------
@ -69,17 +69,17 @@ int main()
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);
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);
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);
}
DrawTextureRec(scarfy, frameRec, position, WHITE); // Draw part of the texture
DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);

View file

@ -13,35 +13,35 @@
#define NUM_FRAMES 3 // Number of frames (rectangles) for the button sprite texture
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite button");
InitAudioDevice(); // Initialize audio device
Sound fxButton = LoadSound("resources/buttonfx.wav"); // Load button sound
Texture2D button = LoadTexture("resources/button.png"); // Load button texture
// Define frame rectangle for drawing
int frameHeight = button.height/NUM_FRAMES;
Rectangle sourceRec = { 0, 0, button.width, frameHeight };
// Define button bounds on screen
Rectangle btnBounds = { screenWidth/2 - button.width/2, screenHeight/2 - button.height/NUM_FRAMES/2, button.width, frameHeight };
int btnState = 0; // Button state: 0-NORMAL, 1-MOUSE_HOVER, 2-PRESSED
bool btnAction = false; // Button action should be activated
Vector2 mousePoint = { 0.0f, 0.0f };
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
@ -49,7 +49,7 @@ int main()
//----------------------------------------------------------------------------------
mousePoint = GetMousePosition();
btnAction = false;
// Check button state
if (CheckCollisionPointRec(mousePoint, btnBounds))
{
@ -59,26 +59,26 @@ int main()
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) btnAction = true;
}
else btnState = 0;
if (btnAction)
{
PlaySound(fxButton);
// TODO: Any desired action
}
// Calculate button frame rectangle to draw depending on button state
sourceRec.y = btnState*frameHeight;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextureRec(button, sourceRec, (Vector2){ btnBounds.x, btnBounds.y }, WHITE); // Draw button frame
EndDrawing();
//----------------------------------------------------------------------------------
}
@ -89,9 +89,9 @@ int main()
UnloadSound(fxButton); // Unload sound
CloseAudioDevice(); // Close audio device
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View file

@ -11,23 +11,23 @@
#include "raylib.h"
#define NUM_FRAMES 8
#define NUM_LINES 6
#define NUM_FRAMES 8
#define NUM_LINES 6
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite explosion");
InitAudioDevice();
// Load explosion sound
Sound fxBoom = LoadSound("resources/boom.wav");
// Load explosion texture
Texture2D explosion = LoadTexture("resources/explosion.png");
@ -36,72 +36,72 @@ int main()
int frameHeight = explosion.height/NUM_LINES; // Sprite one frame rectangle height
int currentFrame = 0;
int currentLine = 0;
Rectangle frameRec = { 0, 0, frameWidth, frameHeight };
Vector2 position = { 0, 0 };
bool active = false;
int framesCounter = 0;
SetTargetFPS(120);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Check for mouse button pressed and activate explosion (if not active)
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !active)
{
position = GetMousePosition();
active = true;
position.x -= frameWidth/2;
position.y -= frameHeight/2;
PlaySound(fxBoom);
}
// Compute explosion animation frames
if (active)
{
framesCounter++;
if (framesCounter > 2)
{
currentFrame++;
if (currentFrame >= NUM_FRAMES)
{
currentFrame = 0;
currentLine++;
if (currentLine >= NUM_LINES)
{
currentLine = 0;
active = false;
}
}
framesCounter = 0;
}
}
frameRec.x = frameWidth*currentFrame;
frameRec.y = frameHeight*currentLine;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw explosion required frame rectangle
if (active) DrawTextureRec(explosion, frameRec, position, WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
@ -109,12 +109,12 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(explosion); // Unload texture
UnloadSound(fxBoom); // Unload sound
UnloadSound(fxBoom); // Unload sound
CloseAudioDevice();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View file

@ -11,32 +11,33 @@
#include "raylib.h"
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] examples - texture source and destination rectangles");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D scarfy = LoadTexture("resources/scarfy.png"); // Texture loading
int frameWidth = scarfy.width/6;
int frameHeight = scarfy.height;
// NOTE: Source rectangle (part of the texture to use for drawing)
// Source rectangle (part of the texture to use for drawing)
Rectangle sourceRec = { 0.0f, 0.0f, frameWidth, frameHeight };
// NOTE: Destination rectangle (screen rectangle where drawing part of texture)
// Destination rectangle (screen rectangle where drawing part of texture)
Rectangle destRec = { screenWidth/2, screenHeight/2, frameWidth*2, frameHeight*2 };
// NOTE: Origin of the texture (rotation/scale point), it's relative to destination rectangle size
// Origin of the texture (rotation/scale point), it's relative to destination rectangle size
Vector2 origin = { frameWidth, frameHeight };
int rotation = 0;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@ -63,7 +64,7 @@ int main()
DrawLine((int)destRec.x, 0, (int)destRec.x, screenHeight, GRAY);
DrawLine(0, (int)destRec.y, screenWidth, (int)destRec.y, GRAY);
DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);
EndDrawing();

View file

@ -1,6 +1,6 @@
/*******************************************************************************************
*
* raylib [textures] example - Retrieve image data from texture: GetTextureData()
* raylib [textures] example - Retrieve image data from texture: GetTextureData()
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
@ -13,12 +13,12 @@
#include "raylib.h"
int main()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture to image");
@ -27,10 +27,10 @@ int main()
Image image = LoadImage("resources/raylib_logo.png"); // Load image data into CPU memory (RAM)
Texture2D texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (RAM -> VRAM)
UnloadImage(image); // Unload image data from CPU memory (RAM)
image = GetTextureData(texture); // Retrieve image data from GPU memory (VRAM -> RAM)
UnloadTexture(texture); // Unload texture from GPU memory (VRAM)
texture = LoadTextureFromImage(image); // Recreate texture from retrieved image data (RAM -> VRAM)
UnloadImage(image); // Unload retrieved image data from CPU memory (RAM)
//---------------------------------------------------------------------------------------