mrToad wrote:...
New Example:
It would be nice to have a fb-translated example of scarfy as part of the examples collection, to show loading and animating sprites from a texture. I will try to do this and contribute it.
...
Code: Select all
/'******************************************************************************************
*
* raylib [textures] example - Texture loading and drawing a part defined by a rectangle
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
*
*******************************************************************************************'/
#include once "../raylib.bi"
/'
This is required only by my custom headers, otherwise the names clash
with some definitions in the OpenGL headers for FreeBasic.
Not needed if you aren't going to use/reimplement more advanced
features like custom shader support.
Nevertheless, they are somewhat similar to those from IchMagBier (it defines
constructors for the most common types like VectorX, Rectangle and Color).
'/
#ifndef RAYRED
#define RAYRED RED
#endif
#ifndef RAYGREEN
#define RAYGREEN GREEN
#endif
#ifndef RAYBLUE
#define RAYBLUE BLUE
#endif
/'
Example follows
'/
#define MAX_FRAME_SPEED 15
#define MIN_FRAME_SPEED 1
dim as const integer _
screenWidth => 800, _
screenHeight => 450
InitWindow( _
screenWidth, screenHeight, _
"raylib [texture] example - texture rectangle" )
'' NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
dim as Texture2D _
scarfy => LoadTexture( "resources/scarfy.png" )
var _
position => Vector2( 350.0f, 280.0f ), _
frameRec => Rectangle( 0.0f, 0.0f, scarfy.width / 6, scarfy.height )
dim as integer _
currentFrame => 0, _
framesCounter => 0, _
framesSpeed => 8 '' Number of spritesheet frames shown by second
'' Set our game to run at 60 frames-per-second
SetTargetFPS( 60 )
do while( not WindowShouldClose() )
framesCounter +=> 1
if ( framesCounter >= ( 60 / framesSpeed ) ) then
framesCounter => 0
currentFrame +=> 1
if (currentFrame > 5) then
currentFrame = 0
end if
frameRec.x => currentFrame * scarfy.width / 6
end if
if( IsKeyPressed( KEY_RIGHT ) ) then
framesSpeed +=> 1
elseif( IsKeyPressed( KEY_LEFT ) ) then
framesSpeed -=> 1
end if
if( framesSpeed > MAX_FRAME_SPEED ) then
framesSpeed => MAX_FRAME_SPEED
elseif( framesSpeed < MIN_FRAME_SPEED ) then
framesSpeed => MIN_FRAME_SPEED
end if
'' Draw
BeginDrawing()
ClearBackground( RAYWHITE )
DrawTexture( scarfy, 15, 40, WHITE )
DrawRectangleLines( 15, 40, scarfy.width, scarfy.height, LIME )
DrawRectangleLines( 15 + frameRec.x, 40 + frameRec.y, frameRec.width, frameRec.height, RAYRED )
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 i as integer => 0 to MAX_FRAME_SPEED - 1
if( i < framesSpeed ) then
DrawRectangle( 250 + 21 * i, 205, 20, 20, RAYRED )
end if
DrawRectangleLines( 250 + 21 * i, 205, 20, 20, MAROON )
next
'' Draw part of the texture
DrawTextureRec( scarfy, frameRec, position, WHITE )
DrawText( "(c) Scarfy sprite by Eiden Marsal", _
screenWidth - 200, screenHeight - 20, 10, GRAY )
EndDrawing()
loop
UnloadTexture( scarfy )
CloseWindow()

Just make sure to change all paths as appropriate and you're set.