Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Copy part of the image to memblock and create another image from this memblock?

Author
Message
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 26th Dec 2009 17:13
Hello once again!
I asked this quastion before and i am asking again. How can i copy part of the image to memblock and create another image from this memblock? Please help...

Dark GDK IS THE BEST!!!!
ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 27th Dec 2009 05:18
Try this:

dbMakeMemblockFromImage(first image)
dbMakeBitmapFromMemblock
dbGetImage(specify which area you want here)
dbMakeMemblockFromImage(output image)
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 28th Dec 2009 14:03
I found your earlier thread:

http://forum.thegamecreators.com/?m=forum_view&t=162622&b=22

Your problem seems to be to display one character of an image which contains a font set. You received the advice to create an animated sprite from it, to which you replied:

Quote: "This command will load and create an animated sprite. And I need to split the sprite into 256 parts and then create 256 sprites from these parts with there own ID numbers."


You don't need to split it into 256 parts. You can simply use the dbSetSpriteFrame command to display the character you need. I believe that is the simplest solution. (Although you really need to create a sprite for each character to be displayed, but I think that will be true for the other alternatives as well.)

Second alternative: Have a look at the Dark Invaders tutorial which comes with the Dark GDK installation. In the explanatory text, you can find a description of how to display characters by assigning texture coordinates to sprites, to make each sprite display only part of a large image.

Third alternative: If you insist on memblocks, there was a bitmap font tutorial in the newsletter 59 which explains that solution:

http://www.thegamecreators.com/data/newsletter/newsletter_issue_59.html#6

Unfortunately the code is for DB Pro and looks complicated as well, but it should be possible to convert the parts you need to Dark GDK.
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 29th Dec 2009 20:17
Oh! Mireben, thank you very MUCH!!! Now i am trying to write a code for bitmap font. I will read this all thank you!

Dark GDK IS THE BEST!!!!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 31st Dec 2009 11:56
Hi Alec, here is another idea for you which occurred to me after I wrote the message yesterday. There is a command dbCopyBitmap, which accepts the area to be copied as a parameter. (Source coordinates, destination coordinates.) You could treat the font set as a bitmap instead of an image and copy just the area to the current screen, which is bitmap zero.

I tested this idea this morning and it seems to work. Loading the bitmap is a bit tricky because dbLoadBitmap would try to load it onto the screen, so I load the image as image, and create a bitmap from it through a memblock:



Then the appropriate area of bitmap 102 (one character) can be copied to bitmap zero (the screen).

The advantage is that you don't need to worry about unique sprite ID numbers for each displayed character. The disadvantage is that you can't scale the font (as you can with a sprite) and you can't make it transparent, but if you are OK with these limitations, then the code will be very simple.

I haven't studied bitmap fonts in depth yet - I will have to at some point later - but I hope one of these ideas will help you.

Happy new year to you!
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 31st Dec 2009 15:15
Hi, Mireben , many thanks for trying help me). Well now i am almost finished my work. I will show the final code I tried many ways and now i know that copying images or something like that will use much memory.

I will calculate the postion of the pixel`s red, blue, green, alpha value and paste it to another memblock. I thought it take alot of time. I was wrong - Dark GDK do it very fast. When i will finish this code i will show it

Dark GDK IS THE BEST!!!!
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 2nd Jan 2010 23:02
Hello I finished the work. Now i can show you the final code i will use in my application.

Dark GDK IS THE BEST!!!!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 3rd Jan 2010 11:05
Great! I'm interested in the code if you are willing to post it here.
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 3rd Jan 2010 14:44 Edited at: 3rd Jan 2010 16:10
class BitMapFont :: CreateSpriteFromFont (void);

private variables:

MemBlockImageID - ID of memblock of bitmap font
RenderMemblockID - ID of memblock of final sprite we will show
ASCii - the ASCII code of the character
CurrentLength - the length of our characters in pixels in our final sprite
CurrentTextDist[]- array that contains the values of each charcter in our sprite




Dark GDK IS THE BEST!!!!
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 3rd Jan 2010 14:51 Edited at: 3rd Jan 2010 16:10
This function is class function.
ASCii - contains the ASCII value of current character

STEP 1:
calculating the position of the character on the bitmap



i == ASCii-31 - first character in position of X=1; Y=1 has ASCii = 32.
after this part of code we have FontChar_X - X position of our characeter on our bitmap FontChar_Y - Y position

FontChar_X = x; FontChar_Y = y;

Dark GDK IS THE BEST!!!!
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 3rd Jan 2010 15:06 Edited at: 3rd Jan 2010 15:08
STEP 2:
copying pixels from original bitmap with all font characters to the memblock


if not space button pressed then:
to AlphaColumn we will write the height of our charcter square in my case it is 32
every time before we copy/paste the values of our pixel i cheking alpha value of all "y" values. If all alpha values of "y" vertical raw are "0" then AlphaColumn will be "0" because

AlphaColumn = S_height;
S_pixel_loc = (x + (FontChar_X-1)*S_height + (i + (FontChar_Y-1)*S_height) * B_width) * 4 + 12; // reading pixel location
if (0 == dbMemblockByte(MemBlockImageID, S_pixel_loc + 3)) AlphaColumn--; // if alpha value "0" then AlphaColumn--

if AlphaColumn greater then 0 we will copy and paste pixel values. but if you wantu can copy only that pixels that have alpha values greater then 0..

Dark GDK IS THE BEST!!!!
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 3rd Jan 2010 15:16
STEP 3:
if space button pressed just add 5 pixels to our sprite (memblock)




Dark GDK IS THE BEST!!!!
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 3rd Jan 2010 15:20 Edited at: 3rd Jan 2010 15:23
STEP 4(FINAL):
will write the distance in pixels in our array
and increase our length in this value
then creating sprite from our memblock




and in another function show it or hide it...
thank`s everyone who treid to read all this if there will be quastions please ask

Dark GDK IS THE BEST!!!!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 5th Jan 2010 19:02 Edited at: 5th Jan 2010 19:04
I tried to understand how it works. I don't yet understand every bit but it's a very nice and very useful function and I will certainly test it in practice, but I have a few questions.

So, you say "MemBlockImageID - ID of memblock of bitmap font". That means that you have an image of the font set which you load as an image and then convert it to a memblock, maybe in the constructor of that class? In any case, you have to do this before this function is called, is that right?

"RenderMemblockID - ID of memblock of final sprite we will show". This memblock also must be created first, maybe with the dbMakeMemblock function? How do you calculate the size of this memblock in advance and how do you make sure that it does not overflow when this function is writing more and more characters into memory?

When you determine the value of AlphaColumn, you check against value zero, so does it mean that the black colour will be transparent?

In this code:



shouldn't the first S_height be S_width? Judging from the comments and from the structure of that for statement.
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 7th Jan 2010 02:53 Edited at: 7th Jan 2010 03:05
Ok i will show you class with all functions.

class FontField - reading keybord




class BitmapFont - converts char arry from "class FontField" into the sprite






The main function And use 512*512 png file for the font




You are wellcome

Dark GDK IS THE BEST!!!!
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 7th Jan 2010 03:14
Mireben Copy all this into your empty Dark GDK project and create bitmap font. And you will see that it works . The bitmap font must be 512*512 *.png file with 16x16 structure. Alphavalue = 0 in not letters pixels.

Dark GDK IS THE BEST!!!!
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 7th Jan 2010 03:17 Edited at: 7th Jan 2010 03:18
You can use this one BitMap Font to test the code.
Click "download" button at the right bottom side of my post You will get my test font.

Dark GDK IS THE BEST!!!!

Attachments

Login to view attachments
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 7th Jan 2010 03:20
Oh... the "BitMapFont.h" is the header file with 2 classes "class FontField" and "class BitmapFont"...

Dark GDK IS THE BEST!!!!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 7th Jan 2010 20:36 Edited at: 7th Jan 2010 21:36
Thank you! I need a few days to have time for any serious testing. I'll give you feedback later. I had only a very quick look-through now.

A warning for your AddChar function: if you use any Dark GDK function which returns char pointer (like dbEntry or dbStr, for example), you have to delete[] the returned string, otherwise you create a memory leak.



What *"" is supposed to be? It occurs in several places. If you want to close a string at a certain place, you can just put a null terminator, i.e. the value zero.
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 7th Jan 2010 23:13
this is like char *_char;
can you show me the example of using "delete[]" in my case? where i should put it?

Dark GDK IS THE BEST!!!!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 8th Jan 2010 21:44
The _char pointer can be deleted when it is no longer needed. Since it is used throughout the function, the deletion can only be at the end:



There are also three early returns and if any of those is executed, the deletion does not happen. To avoid having to write the command three times, you can combine those conditions into one:



If you are interested, I recently took part in improving a function for storing typed characters into a buffer. The finished function is at the end of this thread:

http://forum.thegamecreators.com/?m=forum_view&t=160546&b=22

Your solution is good as well, I mentioned this only because it's good to see several ways of doing something.
AlecG
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 12th Jan 2010 23:15
Oh, Mireben...thank you! I will use "delete[]" for my pointers.

Dark GDK IS THE BEST!!!!

Login to post a reply

Server time is: 2024-11-20 07:25:06
Your offset time is: 2024-11-20 07:25:06