Simple Game framework

Game development specific discussions.
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Simple Game framework

Post by aurelVZAB »

I am not sure why
when i press key "S" my bitmap is not saved ?
any exaplanation?

Code: Select all

sub SaveSprites()
    for i as integer = 0 to nFrames-1
        bsave "C:\FBbitmaps\"+ "newBMP.bmp",sprite(i)
    next i
end sub
well yes this way save something ...it is presented in explorer but just empty image
why is that ?
thanks ..
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Simple Game framework

Post by badidea »

Your links don't work for me.
I tested with path removed and it works fine (tested on Windows with 32 and 64 bit fbc 1.06).

Code: Select all

bsave "tSprite"+str(i)+".bmp",sprite(i)
Valid bitmap files for e.g. MS paint.
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Simple Game framework

Post by aurelVZAB »

Yeah links,because we dont have attachment here ?
however ...as i said before when backslash added to path then path work
BUT saved .bmp image is EMPTY ..even of course i draw something
In GDI on windows is normal to use BitmpInfoStructure to save .bmp image but
how gfx work i am not sure ...
maybe @basicCoder2 have a solution?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Simple Game framework

Post by D.J.Peters »

@aurelVZAB do you know with FBImage you can save and load your sprites ?
var img = LoadRGBAImage(file)
SavePNGFile(fileNam, img, [saveAlphaChannel=false])

FBImage is a lib for PC x86 and x86_64 Windows and Linux (no runtime needed it's static lib)

Joshy
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Simple Game framework

Post by badidea »

I do not understand the problem. I am back on linux now and changed the save line to:

Code: Select all

bsave "/home/badidea/tSprite"+str(i)+".bmp",sprite(i)
And it works as expected, here is my beautiful art: https://nr100.home.xs4all.nl/badidea/sprite_test/
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Simple Game framework

Post by aurelVZAB »

No I don't i even don't know what i this .
But i want to know why given method in this program not work ?
Also i don't need sprite as sprite i need just simple bitmap and i think that this
program can do that , yes looking simple but draw just fine .
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Simple Game framework

Post by aurelVZAB »

@badidea
i don't use linux ,i use windows and that type of path with / not work but work with \
so that is not problem, problem is when i draw image on raster and press key S...image
should be saved , yes image exists in a created folder on C as is in example but image is empty
my drawings is not visible just blank white image
do you understand now?
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Simple Game framework

Post by BasicCoder2 »

@aurelVZAB

Not sure what you want to do. I have no idea about the code behind FB graphics as they are just graphic commands to me. All the simple graphics including animations I have used in any FB program I did using MSPAINT.

With the sprite animator program I was just messing about. It is far from a useful sprite animation program.

The images save and load for me on Windows. I assume you created and chose a folder to save them in. You can just save them in the folder where your program resides with,

Code: Select all

sub SaveSprites()
    for i as integer = 0 to nFrames-1
        bsave "tSprite"+str(i)+".bmp",sprite(i)
    next i
end sub

sub LoadSprites()
    for i as integer = 0 to nFrames-1
        bload "tSprite"+str(i)+".bmp",sprite(i)
    next i
    upDate(1)
end sub
A proper program would have a pop up window with complete directory control to save and load images.
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Simple Game framework

Post by aurelVZAB »

Hi @basicCoder2

I don't want any animated sprites or any "sprite" i just want to make simple bitma editor
using this example ...program ..and i found it simple and good ,
it draw well..
I want it to make simple bitmaps for example
bitmap button, bitmap static control and similar
i found that program tend to use frames ..i dont need frames
then i just do this :

const nFrames = 1 'number of frames

so as you see only one frame ..and then i press key "S" well it do the magic
finally i get .bmp bitmap image in a folder i made on disk C

Code: Select all

sub SaveSprites()
    for i as integer = 0 to nFrames-1
        bsave "C:\FBbitmaps\"+ "newBMP.bmp",sprite(i)
    next i
end sub
as you see i callled it newBMP..and yes is there and all colors work , if i can i will somehow post screenshot
or maybe better post whole code :
ok?
I hope that work on your computers , i compiled it on my win7 machine with FB 1.0.5
so you can test it ...

Code: Select all

screenres 640,480,32
color rgb(0,0,0),rgb(255,255,255)       'white paper, black ink
cls 'implements color command

const nFrames = 1   'number of frames
const wSprite = 32  'width of sprite
const hSprite = 32  'height of sprite
const SIZE    =  12 'size of grid pixel.
const POSX    = 220 'top/left position of grid on screen
const POSY    = 64

dim shared as integer  mx,my,ox,oy,mb   'mouse variables
dim shared as uinteger selectedColor    'current color selected
dim shared as any ptr  sprite(nFrames)  'current sprite image
dim shared as integer  currentFrame     'current image being edited
selectedColor = rgb(0,0,0)
'mb=2

for i as integer = 0 to nFrames-1
    sprite(i) = imageCreate(wSprite,hSprite,rgb(255,255,255))  'frames
next i

'draw palette 1
dim shared as any ptr palette1
palette1 = imagecreate(512,16)
for i as integer = 0 to 31
   line palette1,(i*16,0)-(i*16+15,15),rgb(int(rnd(1)*256),int(rnd(1)*256),int(rnd(1)*256)),bf
next i

'show palette
    put (0,460),palette1,trans

sub upDate(fade as integer)
   
    dim as uinteger r,g,b,p
    'screenlock
    'copy pixel values from sprite to grid display
    line (POSX-2,POSY-2)-(POSX+SIZE*wSprite+2,POSY+SIZE*hSprite+2),rgb(10,10,10),b
    for j as integer = 0 to hSprite-1
        for i as integer = 0 to wSprite-1
           
            p = point(i,j,sprite(currentFrame))
            r = p shr 16 and 255
            g = p shr  8 and 255
            b = p and 255
            line (i*SIZE+POSX+1,j*SIZE+POSY+1)-(i*SIZE+SIZE+POSX-1,j*SIZE+SIZE+POSY-1),rgb(r,g,b),bf
            line (i*SIZE+POSX,j*SIZE+POSY)-(i*SIZE+SIZE+POSX,j*SIZE+SIZE+POSY),rgb(100,100,255),b
           
            if currentFrame<>0 and fade = 1 then  'show previous drawing
                if point(i,j,sprite(currentFrame))=rgb(255,255,255) then
                    p = point(i,j,sprite(currentFrame-1))
                    if p<>rgb(255,255,255) then
                        r = p shr 16 and 255
                        g = p shr  8 and 255
                        b = p and 255
                        r = r*4 and &HFFFFFF
                        g = g*4 and &HFFFFFF
                        b = b*4 and &HFFFFFF
                        line (i*SIZE+POSX+1,j*SIZE+POSY+1)-(i*SIZE+SIZE+POSX-1,j*SIZE+SIZE+POSY-1),rgb(127,127,127),bf
                    end if
                end if
            end if
           
        next i
    next j
   
    locate 38,2
    print "[.] move forward one frame"
    locate 40,2
    print "[,] move back one frame"
    locate 42,2
    print "[S] to save sprites"
    locate 44,2
    print "[L] to load sprites"
    locate 46,2
    print "[C] to clear sprites"
    locate 48,2
    print "[ESC] TO END PROGRAM"
    'locate 50,2
    'print "[A] TO ANIMATE"
   
    'display sprite
    line (4,4)-(37,37),rgb(10,10,10),b
    line (2,2)-(39,39),rgb(10,10,10),b
    put (5,5),sprite(currentFrame),pset
    locate 10,3
    print "FRAME =";currentFrame;"       "
    'screenunlock
   
end sub


sub SaveSprites()
    for i as integer = 0 to nFrames-1
        bsave "C:\FBbitmaps\"+ "newBMP.bmp",sprite(i)
    next i
end sub

sub LoadSprites()
    for i as integer = 0 to nFrames-1
        bload "C:/FBbitmaps"+str(i)+".bmp",sprite(i)
    next i
    upDate(1)
end sub

sub animate(delay as double)
    dim as double now1
    currentFrame = 0
    now1 = timer
    update(0)
    do
        if timer > now1 + delay then
            now1 = timer
            update(0)
            currentFrame = currentFrame + 1
            if currentFrame >= nFrames-1 then
                currentFrame = 0
            end if
        end if
        sleep 2
    loop until multikey(&H01)
end sub


'MAIN PROGRAM

upDate(1)

getmouse mx,my,,mb
ox = mx
oy = my
'if mb = 2 then
    '    selectedColor = point(mx,my)
'end if

dim as integer i,j
dim as string key

do
    key = inkey
   
    if ucase(key) = "S" then
        SaveSprites()
    end if
   
    if ucase(key) = "L" then
        LoadSprites()
    end if
   
    if ucase(key) = "C" then
        for i as integer = 0 to nFrames-1
            line sprite(i),(0,0)-(wSprite,hSprite),rgb(255,255,255),bf
        next i
        upDate(1)
    end if
   
    if ucase(key) = "A" then
        animate(0.05)
    end if
   
    if key = "," then
        currentFrame = currentFrame-1
        if currentFrame < 0 then
            currentFrame = nFrames-1
        end if
        upDate(1)
    end if
   
    if key = "." then
        currentFrame = currentFrame+1
        if currentFrame > (nFrames-1) then
            currentFrame = 0
        end if
        upDate(1)
    end if
   

    getmouse mx,my,,mb
    'select color right click -----------------------
    if mb = 2 then
        selectedColor = point(mx,my)
    end if
    '------------------------------------------------
   
    'over drawing area?
    if mx>POSX and mx<POSX+wSprite*SIZE+SIZE-1 and my>POSY and my<POSY+hSprite*SIZE+SIZE-1 then
        if mb = 1 then
            pset sprite(currentFrame),((mx-POSX)\SIZE,(my-POSY)\SIZE),selectedColor
            update(1)
            while mb=1
                getmouse mx,my,,mb
                if ox<>mx or oy<>my then
                    pset sprite(currentFrame),((mx-POSX)\SIZE,(my-POSY)\SIZE),selectedColor
                    upDate(1)  'copy sprite pixel values to grid, display result with fade
                    ox = mx
                    oy = my
                end if
                sleep 2
            wend
        end if
    end if
       
    sleep 2
   
loop until multikey(&H01)
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Simple Game framework

Post by aurelVZAB »

..just one small thing
I am not very much used to FB and what is this ?

dim shared as any ptr sprite(nFrames) 'current sprite image

is that array of pointers ?
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Simple Game framework

Post by BasicCoder2 »

Then what you want is a Graphics User Interface, a GUI?

There are libraries for doing just that and I think Paul Squires offers something like that for Windows.

viewtopic.php?f=8&t=25215&

There are examples on this forum for rolling your own GUI buttons but you will not have a professional looking program.
You can draw the graphics for buttons and so on with any paint program.

Yes this is the creation of an array of pointers,
dim shared as any ptr sprite(nFrames) 'current sprite image
Actually you should write it as sprite(0 to nFrames-1)
You then assign its value to the address of a created bitmap.
sprite(i) = imageCreate(wSprite,hSprite,rgb(255,255,255))

My suggestion is you read up the FreeBASIC documentation on creating and using bitmaps.

I don't have facebook (or even a smart phone).
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Simple Game framework

Post by aurelVZAB »

I know that i can use any paint program ..i am using most of time Paint.NET
but i want to have small utility program like this one to easy draw some bitmaps.
and yes it is this one is good ..just need some additions like
:
save bitmap using standard SaveFile dialog
load bitmap from file using OpenFile dialog
choose colors from given pallete or something similar
this one could be good example how to do that
maybe would be good to open new topic ?

heh documentation...uff who have time to read all that things
for me is always simplier to look over internet than read docs..
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Simple Game framework

Post by BasicCoder2 »

You can always write your own version based on the above code?
The quick mod below only saves images in the same folder as the program.
It displays any .bmp images found in the folder to select from to load.
Modified the code so you can move the mouse faster without leaving a string of pixels.
You can load a bitmap image of palette colours instead of making a random set.
There are plenty of ways to improve the program coding and what it can do but it is a start.
If you were still interested it would be worth opening as another topic perhaps in the projects section of the forum.

Right mouse button down, Save image as ... , then load into paint program and resave as bitmap palette1.bmp

Image

Code: Select all

chdir exepath()


screenres 640,480,32
color rgb(0,0,0),rgb(255,255,255)       'white paper, black ink
cls 'implements color command

Const NULL As Any Ptr = 0
dim shared as string dirPath      'path to folder containing bitmap images
dirPath = curDir
dim shared as string images(0 to 100)  'list of bitmap images in folder
dim shared as integer MAX_IMAGES = 100

const wImage = 32  'width of Image
const hImage = 32  'height of Image
const SIZE    =  12 'size of grid pixel.
const POSX    = 220 'top/left position of grid on screen
const POSY    = 8

dim shared as integer bmCount
dim shared as integer  mx,my,ox,oy,mb   'mouse variables
dim shared as uinteger selectedColor    'current color selected
selectedColor = rgb(0,0,0)

'draw palette 1
dim shared as any ptr palette1
palette1 = imagecreate(153,96)
bload "palette1.bmp",palette1


'make Image image
dim shared as any ptr Image
Image = imagecreate(32,32,rgb(255,255,255))

sub makeImagesList()
    bmCount = 0
    dim as string file
    CONST attrib_archive    = 32
    CHDIR dirPath  'Change this to the directory you want to browse
    file = dir("*", attrib_archive)
'    'get first image
    if mid(file,len(file)-3,4) = ".bmp" then
        images(bmCount)=file
        bmCount = bmCount + 1
    end if
    'get the rest of the images
    do
        file = dir("", attrib_archive)
        if mid(file,len(file)-3,4) = ".bmp" then
            images(bmCount)=file
            if bmCount<MAX_IMAGES then
                bmCount = bmCount + 1
            end if
        end if
    loop while file <> ""
end sub

sub upDate()
   
    dim as uinteger r,g,b,p
    screenlock
    cls
    'copy pixel values from Image to grid display
    line (POSX-2,POSY-2)-(POSX+SIZE*wImage+2,POSY+SIZE*hImage+2),rgb(10,10,10),b
    for j as integer = 0 to hImage-1
        for i as integer = 0 to wImage-1
           
            p = point(i,j,Image)
            r = p shr 16 and 255
            g = p shr  8 and 255
            b = p and 255
            line (i*SIZE+POSX+1,j*SIZE+POSY+1)-(i*SIZE+SIZE+POSX-1,j*SIZE+SIZE+POSY-1),rgb(r,g,b),bf
            line (i*SIZE+POSX,j*SIZE+POSY)-(i*SIZE+SIZE+POSX,j*SIZE+SIZE+POSY),rgb(100,100,255),b
           
        next i
    next j
   
    locate 32,2
    print "[S] to save image"
    locate 34,2
    print "[L] to load image"
    locate 36,2
    print "[C] to clear image"
    locate 38,2
    print "[ESC] TO END PROGRAM"

    'display Image
    line (4,4)-(37,37),rgb(10,10,10),b
    line (2,2)-(39,39),rgb(10,10,10),b
    
    put (5,5),Image,pset
    put (8,360),palette1,trans
    line (8,360)-(8+152,360+95),rgb(0,0,255),b
    'draw select color
    line (170,416)-(170+30,416+30),selectedColor,bf
    line (170,416)-(170+30,416+30),rgb(0,0,0),b
    screenunlock
   
end sub


sub SaveImage()
    cls
    locate 2,2
    dim as string fileName
    INPUT "ENTER Image FILE NAME:";fileName
    if right(fileName,4)<>".bmp" then
        fileName = fileName + ".bmp"
    end if
    bsave fileName,Image
end sub

sub LoadImage()
    cls
    dim as string fileName
    makeImagesList()
    'print list of images
    locate 1,1
    for i as integer = 0 to bmCount-1
        print images(i)
    next i
    print
    INPUT "ENTER Image FILE NAME:";fileName
    if right(fileName,4)<>".bmp" then
        fileName = fileName + ".bmp"
    end if
    bload fileName,Image
end sub


upDate()

getmouse mx,my,,mb
ox = mx
oy = my

dim as integer i,j
dim as string key

do
    key = inkey
   
    if ucase(key) = "S" then
        SaveImage()
    end if
   
    if ucase(key) = "L" then
        LoadImage()
    end if
   
    if ucase(key) = "C" then
            line Image,(0,0)-(wImage,hImage),rgb(255,255,255),bf
        upDate()
    end if
   

    getmouse mx,my,,mb

    'over drawing area?
    if mx>POSX and mx<POSX+wImage*SIZE+SIZE-1 and my>POSY and my<POSY+hImage*SIZE+SIZE-1 then
        if mb = 1 then
            pset Image,((mx-POSX)\SIZE,(my-POSY)\SIZE),selectedColor
            ox = mx
            oy = my
            update()
            while mb=1
                getmouse mx,my,,mb
                if ox<>mx or oy<>my then
                    line Image,((mx-POSX)\SIZE,(my-POSY)\SIZE)-((ox-POSX)\SIZE,(oy-POSY)\SIZE),selectedColor
                    upDate()
                    ox = mx
                    oy = my
                end if
                sleep 2
            wend
        end if
    end if

    'is mouse over palette?
    if mx>8 and mx<152 and my>360 and my<360+95 then
        if mb=1 then
            selectedColor = point(mx,my)
        end if
    end if
    
    
    update()
       
    sleep 2
   
loop until multikey(&H01)
Post Reply