Dark Bit Factory & Gravity

PROGRAMMING => Coding tutorials => Topic started by: Shockwave on December 15, 2008

Title: Freebasic Images Library
Post by: Shockwave on December 15, 2008
A lot of people post here having grabbed a copy of freebasic and they decide to do some pixel pushing with Tinyptc..

Well, we know what they say about all good intentions.
Actually it can be quite a daunting task if you have not done any software rendering before.

The first thing you need to make sure of is that you have the newest version of tinyptc.
Get it from our resources section and ask on the forum if you don't know how to install it (it's just copying 3 files, don't worry!).

The new version of tinyptc has extra compatibility and functionality, rbz keeps working on it to improve it. Make sure you keep it up to date if you want to use these frameworks.

Most small intros have thier graphics resourced as part of the executable file.
So how the hell do you do it?

You will need to have some utilities, rbz wrote two great programs;

bmp2raw
bin2bas

They are included in this archive and also available from our resources section.

bmp2raw first of all takes a picture (windows 8 bit bitmap) and splits it into two parts.
The first part is the colour palette.
The second part is the raw data.

We need a picture to work on.

Our example is called screen.bmp

Screen.bmp was created in microsoft paint as a 24 bit windows bitmap.
the whole picture is 512  X 172 pixels.

This was then converted into an 8 bit picture with irfanview.

Irfanview is a free image conversion program.
Get it here;

http://www.irfanview.com/

To convert a 24 bit image to 8 bit, load irfanview.
Load your image
go to the image tab in the top menu
go to decrease colour depth
select 256 colours
then save the file as screen.bmp

Simple as that.
You should not try and use 24 bit pictures, it's possible (and fairly easy) to use them but we're sticking to 256 colours here because that's what rbz's utilities are designed for.


to convert the picture into the right format for our program...
Save the 256 colour bitmap as:
screen.bmp
make sure that you have a copy of bin2bas and bmp2raw in that folder too


go to the start button
click run
type:
cmd
into the box
then use cd (whatever) to get into the folder containing screen.bmp
then type;
bmp2raw screen.bmp
then after a few seconds the pic will be converted into 2 files.
Then type;
bin2bas screen.bmp.pal screenpal.bas
bin2bas screen.bmp.raw screenraw.bas

That's the picture converted.


You can now use the files ;
screenpal.bas
screenraw.bas

in your programs.

You should look on this the same way you would if you were cutting out cookies ;)

The source I included loads the picture and provides you with a comand that lets you cut out any part of that picture and display it anywhere on your screen!

So you can put logos, fonts whatever you like on that screen and draw them with ease.

Please do enjoy it, and don't feel bad about asking any questions, they should be posted in here.

Title: Re: Freebasic Images Library
Post by: DrewPee on December 15, 2008
A huge thank you for this Shockwave! As you know I do sometimes struggle with certain bits and bobs!
This will help me and lots of others out too! Very kind of you to do this!
Regards
DrewPee
Title: Re: Freebasic Images Library
Post by: Rbz on December 15, 2008
Reading this tutorial I got an idea, how about integrating an image loader for tinyptc_ext ?
It's not too much code to add, and it could make newcomers live a bit more easy.

What do you guys think about it ?
Title: Re: Freebasic Images Library
Post by: Shockwave on December 16, 2008
It should give the option of resourcing the file into the exe or having it seperate but I think it would give newbies a huge help.

Not everyone wants to go through the pain of converting images and writing thier own bitmap drawing routines etc.

Great idea Rbz :)
Title: Re: Freebasic Images Library
Post by: Hotshot on December 16, 2008
Very good Framework for Newbies  :)
Title: Re: Freebasic Images Library
Post by: Hezad on December 16, 2008
Ah very cool :) k++ shockie, that'll definitely be helpful (for newbies or not :P)
Title: Re: Freebasic Images Library
Post by: Shockwave on December 16, 2008
Cheers for the Karma :D
Title: Re: Freebasic Images Library
Post by: bikerboy on December 16, 2008
a great hand of help indeed !  :cheers:  :D :cheers:

EDIT: just loaded in FBIde "images.bas" to study the example and see how it works, however everytime i try to compile a code containing OPTION STATIC and OPTION EXPLICIT , i get this -->

Quote
error 135: Only valid in -lang deprecated or fblite or qb, found 'OPTION' in 'OPTION STATIC'

error 135: Only valid in -lang deprecated or fblite or qb, found 'OPTION' in 'OPTION EXPLICIT'

is there something my freebasic installation is missing? anyone knows how to fix this?

thanx guys :)
Title: Re: Freebasic Images Library
Post by: DrewPee on December 17, 2008
@Bikerboy - Not sure if this is right but I just removed them! The routines still work fine! It is something to do with an older version of FB.

DrewPee
Title: Re: Freebasic Images Library
Post by: Shockwave on December 17, 2008
You won't hurt anything using the fix Drewpee suggested there, it is indeed because you have a newer version of Freebasic than I do.
Title: Re: Freebasic Images Library
Post by: bikerboy on December 17, 2008
allready tried that and got more errors, i even did it with other code examples with the same results .... what version do you guys have? i think i need to downgrade, this new version is for the trashcan.
Title: Re: Freebasic Images Library
Post by: Shockwave on December 17, 2008
What errors did you have?
Title: Re: Freebasic Images Library
Post by: bikerboy on December 17, 2008
Code: [Select]
Compiler output:
C:/Documents and Settings/bikerboy/Desktop/images2/screenpal.bas(2) error 4: Duplicated definition, found 'screen' in 'dim shared screen.bmp.pal(0 to 767) as ubyte = { _'
C:/Documents and Settings/bikerboy/Desktop/images2/screenraw.bas(2) error 4: Duplicated definition, found 'screen' in 'dim shared screen.bmp.raw(0 to 88063) as ubyte = { _'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(36) error 135: Only valid in -lang deprecated or fblite or qb, found 'OPTION' in 'OPTION STATIC'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(37) error 135: Only valid in -lang deprecated or fblite or qb, found 'OPTION' in 'OPTION EXPLICIT'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(125) error 136: Default types or suffixes are only valid in -lang deprecated or fblite or qb in 'DIM L'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(149) error 136: Default types or suffixes are only valid in -lang deprecated or fblite or qb in 'DIM L'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(210) error 7: Expected ')', found '.' in 'IMG_COLOURS(L)=RGB(screen.bmp.pal(RR),screen.bmp.pal(GG),screen.bmp.pal(BB))'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(220) error 3: Expected End-of-Line, found '.' in 'IMG_RAW(L)=screen.bmp.raw(L)'

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc:   FreeBASIC Compiler - Version 0.20.0 (08-10-2008) for win32 (target:win32)
OS:    Windows XP (build 2600, Service Pack 3)
Title: Re: Freebasic Images Library
Post by: Shockwave on December 17, 2008
It seems like your version of freebasic doesn't like the variable names of the arrays.

screen.bmp.pal
screen.bmp.raw

So to start off with, please load the following files into your IDE;

screenpal.bas
screenraw.bas

and the main program file too, whatever that is called.

go to the first line of screenpal.bas

change
screen.bmp.pal
to;
screenbmppal

Save screenpal.bas

go to the first line of screenraw.bas

change
screen.bmp.raw
to;
screenbmpraw

Save screenraw.bas

Then in the main program file replace every
screen.bmp.pal
with
screenbmppal
and every
screen.bmp.raw
with
screenbmpraw


Then comment out;

option static
option explicit

Then try compiling it, you might get other errors, if so, please post them.
Title: Re: Freebasic Images Library
Post by: bikerboy on December 17, 2008
changed everything you said and --->

Code: [Select]
Compiler output:
C:/Documents and Settings/bikerboy/Desktop/images2/screenpal.bas(2) error 4: Duplicated definition, found 'screen' in 'dim shared screen.bmppal(0 to 767) as ubyte = { _'
C:/Documents and Settings/bikerboy/Desktop/images2/screenraw.bas(2) error 4: Duplicated definition, found 'screen' in 'dim shared screen.bmpraw(0 to 88063) as ubyte = { _'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(125) error 136: Default types or suffixes are only valid in -lang deprecated or fblite or qb in 'DIM L'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(149) error 136: Default types or suffixes are only valid in -lang deprecated or fblite or qb in 'DIM L'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(210) error 7: Expected ')', found '.' in 'IMG_COLOURS(L)=RGB(screen.bmppal(RR),screen.bmppal(GG),screen.bmppal(BB))'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(220) error 3: Expected End-of-Line, found '.' in 'IMG_RAW(L)=screen.bmpraw(L)'
Title: Re: Freebasic Images Library
Post by: Shockwave on December 17, 2008
Well theres less errors but you still need to change;

screen.bmppal

to

screenbmppal

and

screen.bmpraw

to

screenbmpraw


Everywhere you see them.
It seems like Freebasic 2.0 doesnt like those full stops in array names
Title: Re: Freebasic Images Library
Post by: bikerboy on December 17, 2008
okies replaced everything and now got only 2 errors

Code: [Select]
Compiler output:
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(125) error 136: Default types or suffixes are only valid in -lang deprecated or fblite or qb in 'DIM L'
C:/Documents and Settings/bikerboy/Desktop/images2/FBIDETEMP.bas(149) error 136: Default types or suffixes are only valid in -lang deprecated or fblite or qb in 'DIM L'
Title: Re: Freebasic Images Library
Post by: Shockwave on December 17, 2008
At line 125 replace
DIM L
with
DIM AS INTEGER L

At Line 149 do the same

:)

It will work after that. (fingers crossed)
Title: Re: Freebasic Images Library
Post by: bikerboy on December 17, 2008
changed it at line 149 too , and it works ! :carrot:

thanx man, this was an opportunity to learn how to fix specific errors :D

i'll go now and try to fix the oracle one
Title: Re: Freebasic Images Library
Post by: matthew on December 17, 2008
You don't have to change the code in order to get it to compile with the newest version of FreeBASIC.

If you're using FBIde, you just need to add -lang deprecated to your Compiler command & all the examples written in earlier versions of FreeBASIC should compile fine without any errors.

(http://matthew-4gl.wikispaces.com/space/showimage/resized_fbide_view_settings.png)

(http://matthew-4gl.wikispaces.com/space/showimage/resized_fbide_compiler_command.png)

Title: Re: Freebasic Images Library
Post by: Shockwave on December 17, 2008
Have some good Karma Matthew :)
Title: Re: Freebasic Images Library
Post by: bikerboy on December 17, 2008
thanx matthew for the info  :goodpost:

but since i'm in the learning process of freebasic i like to go the "hard" way and change the code, this way i will understand more things about freebasic and errors ;) :D