Since posting exes as new user isn't very popular here i'm gonna do what expected to do, sharin some codes, ideas and stuff. I really love purebasic and i'm not going to stop using it because of my opinion on some people...
Not so far ago, just starting learning purebasic, i've managed to create a sokoban game and soon needed to enhance graphics.
I was looking in this forum to get some ideas for an animated background based on GIF images.
Probably most of you had this idea but i've only readed people talking about GIF LIBRARIE or such...
I've tried it but source code was too long and yes i'm still using demo version so i was unable to run it... then come
to the conclusion that an animation is so easy to do with purebasic sprites that it wont take more than a few code lines.
This little demo shows easy way to do it wich probably will be useful for newbis (as i am)
The code won't work unless it loads the .jpg at the data folder. I'm showing here but need to download at
https://mega.nz/#!hC4xBRgK!KJB-haHw98mm ... pONK03t08Q
then unzip to it's own folder.
More info at the source.
Code: Select all
; ANIMATED GIF BACKGROUNDS By SERGIO 'ManOwaR' SOEIRO 2017
;
;
; * Since i was searching some way to have animated backgrounds in my games, i looked at purebasic forums and people agree to use some
; "gif library" or such.... i've tried it but it has thousands lines so i made this to get a simple and effective way to animate backgrounds.
;
; * This little code is inteded to extract frames from a picture then 'mount' an animated gif using purebasic commands easyly.
; * You should have your animation in a 'comic strip' format. There are tools online to convert gifs to strip etc.
; * You must know data like resolution, frames total and animation frame speed.
; * There are limitation because mounting a picture in a strip format will result in a very long width picture, that probably will crash on certain
; cpus or be loaded with color errors. In this demo i'm using a 3000 pixels width picture in 10 frames (300px each frame)
; Of course image can also be mounted in a "tileset" form but that should be done manually because i haven't found tools at the moment to do it
; and also if you get a tileset (squared) you need to change the algorithm to extract tiles (frames). I've alreday done it for another purposes
; like having a personaliced sprite-based font and that algorithm could be used here if needed.
;
; * This demo works with prewrited data of the animation showed, the way to use several "gifs" must be very easy if you know a little coding
; Here are my suggestions:
; 1: You can have a stored data lines containing your gifs data like ID, NAME, FRAMES TOTAL, WIDTH, HEIGHT
; 2: You can create your own rutine to read 'on the fly' gif data. Pictures must have the data contained in the name, then use string commands
; to extract Data anytime a picture is loaded. an example of this will be a pictured named "#1#WATER BACKCROUND#11#320#240#.JPG" Then you extract
; data splited with "#" and can manage as many of your gifs you put in a folder.
;
; HOPE THIS IS USEFUL TO SOMEBODY
;
;
; EMAIL:sergiomanowarsoeiro@gmail.com
; Greetings for people at dbfinteractive.com
InitSprite()
InitMouse()
InitKeyboard()
UseJPEGImageDecoder()
OpenScreen(640,480,32,"ANIMATED GIF BACKGROUNDS",#PB_Screen_SmartSynchronization,60)
LoadSprite(1,"DATA/BACKGROUNDS/BG 1.jpg")
; VARS
gif_frames=10:;WARNING ! As count starts by zero, frame 1 will be 0 and last frame wil be gif_frames-1
gif_width=300
gif_height=199
gif_frame.f=0
gif_speed.f=0.5
LOOP:
; EXTRACT FRAME
ClipSprite(1,Int(gif_frame)*gif_width,0,gif_width,gif_height)
; DRAW
FlipBuffers()
ZoomSprite(1,640,480)
DisplaySprite(1,0,0)
StartDrawing(ScreenOutput())
DrawText(0,0,"Press [ESC] to exit...")
DrawText(300,30,"FRAME "+Str(Int(gif_frame))+"/"+Str(gif_frames-1))
DrawText(300,45,"SPEED "+Str(gif_speed*100)+"%")
DrawText(0,450,"[-]: Decrease SPEED")
DrawText(0,465,"[+]: Increase SPEED")
StopDrawing()
; UPDATES
gif_frame+gif_speed
If gif_frame>gif_frames-gif_speed:gif_frame=0:EndIf
; INPUTS
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape):End:EndIf
If KeyboardPushed(#PB_Key_Add):gif_speed+0.01:If gif_speed>1:gif_speed=1:EndIf:EndIf
If KeyboardPushed(#PB_Key_Subtract):gif_speed-0.01:If gif_speed<0.1:gif_speed=0.1:EndIf:EndIf
Goto LOOP
I had put several animated backgrounds in my sokoban game wich i'm not sharing here cos it's a diabolic exe but maybe some day restarted to write and share some of the source or the .pbi file at least.
c ya