Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: Clyde on September 07, 2009
-
Whilst the site was offline, I had a brief one to one via email with Jim'll Fix it, about converting over a bitmap scroller routine, the one I picked is the one that Shockwave showed me and uses. This coincides with my questions on classes and pointer types.
Here's what was discussed during the emails.
Hi Jim,
This is a conversion from a Blitz / Freebasic routine that Shockwave uses, and it uses mid$ which i've attempted in C++.
I am having problems with the message settings / setup found in gfx_text, when I look at the locals the message isnt correct and has weird characters at the end.
I've attached all the seperate sources if you need to compile them as a project.
Cheers and all the very best to you,
Clyde.
Hi Clyde,
This might work for you:
char *mid_string(char *input, int start_pos, int stop_pos )
{
static char workspace[256];
int length = stop_pos - start_pos;
strncpy(workspace, input+start_pos, length);
workspace[length]='\0';
return workspace;
}
Problem with using 'new' is you waste memory for every single time you call the function.
Best way would be not to use mid_string() at all, instead find the start pos in the string and draw the next 20 characters, stopping if you hit a character with value 0.
-
I'd be interested to see how that method would be Jim dude, im a little fuzzy wuzzy as after each word there's a space / character 0.
Cheers for your help,
Clyde.
<edit by Jim>
Here are your questions, so now everyone knows what you said
Cheers Jim! :)
Sweet it's working! :D
I had to change strncpy to strncpy_s, as for some reason it warned me that it was unsafe.
and I needed to change length to equal start_pos + stop_pos.
I presume that workspace[length]='\0'; means to return a character of 0 or in this a space.
I've tried to make workspace a static outside of the function with the correct length of the scroller text, but I kept getting cant declare workspace with size 0, plus saying it needs to be constant.
I've declared scroller in the following fashion:
char scroller[] = (" " // 48 spaces.
"HELLO THERE! IS THAT YOU? GUESS WHAT CHUM? "
"I THINK IT'S WORKING THIS SCROLL OF MINE!! YIPPEEE!!!"
" "); // 48 spaces);
Im keen on this idea of finding the characters upto 20, or in how i've got it 48. I'd really like to see how thats done, as i either have a slow pc, or the method with mid_string is slowing down the text output like you mentioned. I have found this the same in free basic too with using anything string related.
Cheers and all the very best,
Mike.
-
I needed to change length to equal start_pos + stop_pos
That doesn't make any sense, so something else must be wrong.
Say you passed in start=5 and stop=15, then length should be 10, not 20. And then when you get to start=200, stop=210 then you would be copying 410 chars instead of 10. That would make it crash because there's only space for 250 in the workspace.
I had to change strncpy to strncpy_s, as for some reason it warned me that it was unsafe
Ah, yes, Microsoft supply an ISO extension to the C runtime library (called the Safer C Library) which makes all the string functions secure. Mostly they're the same function names, but with _s on the end). Microsoft in their infinite wisdom have then gone and made the compiler warn every time you use the normal library routines which is a pain in the neck.
If you go to the Project Properties, and under the Compiler options there's a Preprocessor box. In that box you can add _CRT_SECURE_NO_DEPRECATE to turn off the warnings. Or you can ignore the warnings. Or you can use the new functions, like you have already done so.
'\0' is the same as 0. Strings in C are a series of characters followed by a 0.
As I said, there's no reason to use this midstring function. Why not go
scroller[]="...";
start_pos=0;
...
xpos=0;
for (int x=0; x < number_of_chars_that_will_fill_screen; x++)
{
if (scroller[start_pos+x]=='\0') break;
print_char(xpos, ypos, scroller[start_pos+x]);
xpos+=charwidth;
}
start_pos++;
There's no midstring in there, but it has the same result.
It also means you can do scrollers where the characters are not fixed width
int x=start_pos;
for(;;) //this means "forever", the 'breaks' will exit the loop
{
if (scroller[x]=='\0') break;
print_char(xpos, ypos, scroller[x]);
xpos+=charwidth[x];
if (xpos>SCREEN_WIDTH) break;
x++;
}
That will stop when the rightmost character goes off the screen.
I've tried to make workspace a static outside of the function with the correct length of the scroller text, but I kept getting cant declare workspace with size 0, plus saying it needs to be constant.
I don't understand why you would want to do that, and I can't tell why you need to do that or really what you mean.
Jim
-
Thanks for the info etc dude! :)
I've tried the non mid_string bits, but it goes bananas with the speed, and then crashes out.
-
If you post code then it can be fixed.
I suspect you have 'start_pos' as a float so you can increment by much less than one character at a time - that deals with the speed.
I also suspect you never check that start_pos is past the end of the string (I didn't show that bit).
ie. this will wrap it back to the start
start_pos++;
if (start_pos > strlen(scroller)) start_pos = 0;
Jim
-
Ok, cool didnt spot your recent edit. I've attached a source listing, it also doesnt take into account the scroll_speed.
Cheers,
Clyde.
-
Any ideas on incorparating scroll_speed? As it goes like the clappers, and can't read it.
Cheers,
Clyde.
-
Why not make scroll_pos and scroll_speed floats? Then, you'll probably need to use
(int)scroll_pos
in some places to round it down, when you want to use it as an array index.
Jim
-
I need to have a rethink on this, as soon as i have my new birthday PC almost configured.
-
@JIM: Do you mean make x a float, as I dont see any scroll_pos.
I've done this, but it's still way too quick.
void bitmap_scroller( gfx_buffer *dest, anim_image *font )
{
int character;
int pos_x=0;
for (int x=0; x < 48; x++)
{
if (scroller[ start_pos+x]=='\0') break;
character=( int ) scroller[ start_pos+x ] - 32;
draw_gfx_buffer( dest, font->frame[ character ], pos_x, 240 );
pos_x+=font->frame[ character ]->wwidth;
}
//delay++;
//if ( delay >=4 )
//{
start_pos++;
// delay=0;
//}
if ( start_pos >= (int)strlen(scroller)-32 ) start_pos = 0;
float x= start_pos;
//float xx=(float) x;
for(;;) //this means "forever", the 'breaks' will exit the loop
{
if (scroller[ (int) x]=='\0') break;
character=(int) scroller[ (int) x ] - 32;
draw_gfx_buffer( dest, font->frame[ character ], pos_x, 240 );
pos_x+=font->frame[ character ]->wwidth;
if (pos_x>dest->wwidth) break;
x+=1.25;
}
}
Im a tad lost. :(
Could you convert the bitmap scroller that I first posted that is Freebasic / Blitz based into ( quick alternatives ) in C++ please dude? I only ask as I've been pondering on this for quite some weeks now, and once I see it working it'll complete the puzzle for me ( sink into the grey matter ).
Cheers and all the very best,
Clyde.
-
You're still increasing "start_pos" by 1 every frame.
Try it this way:
float start_pos= 0.0f;
...
start_pos+-=0.1f;
...
int x= start_pos;
for(;;)
{
if (scroller[x]=='\0') break;
...
This will draw your characters at an integer position which is nearest to "start_pos",
thus increasing the actual pixel position every tenth (due to 0.1) frame.
If your font has proper anti-aliasing one might even think about drawing bitmaps at subpixel precision.
-
Cheers HellFire :)
Will try that out, and I haven't got around to adding aa to the font yet.
Still no joys, it's either far too jerky or far too quick. also just noticed it doesnt go off the left edge smoothly.
-
Can you attach the current source including project- and data-files ?
-
Sure thing :)
Here are the sources.
Add libtinyptcmmx.lib and libtinyptc_ext to the linker.
Also add to the project directories in includes the lib - graphics 2d.h / tinyptc_ext.h
-
I fiddled around in your code a bit:
//
// includes.
//
#include <windows.h>
#include <string.h>
#include "tinyptc_ext.h"
#include "lib - graphics 2d 1-1.h"
extern unsigned char us64_pal[];
extern unsigned char us64_raw[];
//
// sub routines.
//
void bitmap_scroller( gfx_buffer *dest, anim_image *font );
//
// meet the globals.
//
static float start_pos=0.0f;
static int delay=0;
static int best_fit_wwidth=48;//640+128/64;
static char scroller[] = { " "
"HELLO THERE! IS THAT YOU CLYDE? GUESS WHAT CHUM? "
"I THINK IT'S WORKING THIS SCROLL OF MINE!! YIPPEEE!!!"
" "};
static int scroller_length=strlen( scroller );
static gfx_buffer *screen_buffer, *temp, *font_image, *scroll_buffer;
static anim_image *font_one;
int main()
{
//
// initializing.
//
screen_buffer=create_gfx_buffer( 640, 480 );
scroll_buffer=create_gfx_buffer( 640+64, 64 );
font_image=load_gfx_buffer( us64_raw, us64_pal, 512, 512 );
font_one=create_anim_images( font_image, 64,64,32,8,8);
delete_gfx_buffer (font_image);
set_graphics( "bItMaP sCrOlLeR", 640, 480 );
//
// main program loop.
//
while (1)
{
//
// clear screen.
//
clear_gfx_buffer(screen_buffer, 0);
bitmap_scroller( screen_buffer, font_one );
//
// render to the screen buffer.
//
ptc_update( &screen_buffer->pixels[0] );
}
delete_anim_image( font_one );
delete_gfx_buffer( screen_buffer );
}
void bitmap_scroller( gfx_buffer *dest, anim_image *font )
{
int curchar= 0;
int x= start_pos;
// find first character that's not outside the left border
while (scroller[curchar] && x+font->frame[scroller[curchar] - 32]->wwidth<0)
{
x+=font->frame[scroller[curchar] - 32]->wwidth;
curchar++;
}
// draw characters until right border reached
while (x<640 && scroller[curchar])
{
char c= scroller[curchar] - 32;
draw_gfx_buffer( dest, font->frame[c], x, 240 );
x+=font->frame[c]->wwidth;
curchar++;
}
start_pos-=1.0f;
}
It now keeps track of the pixel-position of the first character of the scroll-text on the screen.
Characters outside the left border of the screen are skipped, then characters are drawn until the right border of the screen is reached.
This has some potential for optimization if you happen to have a few megabytes of text.
Still misses wrapping when end of the text is reached.
-
I really appreciate the help with this guys.
However it's still a tad jerky, maybe this is because it's using an unproportional font.
I have found in blitz and freebasic with the mid$ approach it slows down stuff.
How'd i make the latest method wrap please dude?
Hugest of thanks for your time and patience,
Clyde.
-
it's still a tad jerky
It's running pretty smooth here.
Try if it works better in a smaller resolution.
Although the scroller is rather simple, you still have to send a 640x480-buffer every frame and your pc might not be making it.
How'd i make the latest method wrap please dude?
The first while-loop figures out which character is the first that needs to be drawn.
If that's the last character of your string you can reset "start_pos" to zero.
-
Since Clyde has used this framebuffer lib in Freebasic before his pc should be ok with updating the full buffer at a decent rate. Perhaps it could be something else running on his system and fighting for cpu time, I assume that its jerky in both debug and release mode ?
-
I've used TinyPTC_Ext with Free Basic, but when using it in C++, im not sure if it will perform the same or less so; as ive only been working in C for just over a month. I only really use the release build, and that is what has the slight jerkyness in.
Perhaps I could get some speed when you use a pointer to be the destination, eg the screen. and the colour info from the srce, again as a pointer, but im a tad stuck in that department, as im getting green pixels whenever I attempt it.
-
When I was a little bored yesterday I modified your draw_gfx_buffer function to extract the visible subregion of your source-image so you don't need to check every single pixel for being off-screen.
void draw_gfx_buffer( gfx_buffer *dest, gfx_buffer *srce, int pos_x, int pos_y )
{
int x, y;
int sx,sy; // top-left of visible source image
int w, h; // width and height of visible source image
// completely off?
if (pos_x >= dest->wwidth
|| pos_y >= dest->height
|| pos_x <= -srce->wwidth
|| pos_y <= -srce->height)
return;
// partially off?
if (pos_x < 0) // left
{
sx= -pos_x;
w= srce->wwidth - sx;
pos_x= 0;
}
else
{
sx= 0;
w= srce->wwidth;
}
if (pos_y < 0) // top
{
sy= -pos_y;
h= srce->height - sy;
pos_y= 0;
}
else
{
sy= 0;
h= srce->height;
}
if (w+pos_x > dest->wwidth) // right
w= dest->wwidth-pos_x;
if (h+pos_y > dest->height) // bottom
h= dest->height-pos_y;
unsigned int *src= srce->pixels + sy*srce->wwidth + sx;
unsigned int *dst= dest->pixels + pos_y*dest->wwidth + pos_x;
for ( y=0; y< h; y++)
{
for ( x=0; x< w; x++)
dst[x]= src[x];
dst+= dest->wwidth;
src+= srce->wwidth;
}
}
-
Awesome stuff dude and thanks very much.
-
I tried this for wrapping, but it's wrong.
if (curchar > scroller_length) start_pos=0;
-
Almost certainly you want >= instead of >
In C, if you have 5 chars in a string, it looks like this
char *message = "abcde";
But in memory, it looks like
a b c d e '\0'
That is, there are 6 chars, the 6th one being decimal 0. That's how C knows where the end of the string is. The length of this string, strlen(message) is 5. If you check message[5] the answer is 0. You are saying
if (curchar > scroller length)
ie. if curchar = 6. If it's got that far, then message[6] has been read and we shouldn't have done that because it doesn't exist and/or could be anything - whatever is in memory after our message string.
Jim
Jim
-
Thanks man! :)
There must be gremlins on my PC, as i've gone back to this, but my font isnt displaying properly like before, it's got lime green borders around the letters that suggest errors someplace.
this is the bitmap text routine:
void update_bitmap_scroller( gfx_buffer *dest, anim_image *font, int pos_y )
{
int curchar= 0;
int x=(int) start_pos;
// find first character that's not outside the left border
while (scroller[curchar] && x+font->frame[scroller[curchar] - 32]->wwidth<0)
{
x+=font->frame[scroller[curchar] - 32]->wwidth;
curchar++;
// doesnt work.
//if (curchar >= scroller_length) start_pos=0;
}
// draw characters until right border reached
while (x<dest->wwidth && scroller[curchar])
{
char c= scroller[curchar] - 32;
draw_gfx_buffer2( dest, font->frame[c], x, pos_y );
x+=font->frame[c]->wwidth;
curchar++;
}
start_pos-=scroll_speed;
}
And here's the routines used for loading from memory:
//classess.
class gfx_buffer
{
public:
int wwidth, height;
int wwidth2, height2;
unsigned int *pixels;
};
class anim_image
{
public:
int frame_wwidth, frame_height;
int total_frames;
gfx_buffer **frame;
};
gfx_buffer *load_gfx_buffer(unsigned char *raw, unsigned char *pal, int wwidth, int height)
{
//round width up to next multiple of 4
wwidth += ( 4-( wwidth&3 )) & 3;
//height += ( 4-( height&3 )) & 3;
//create a new image
gfx_buffer *buffer = create_gfx_buffer( wwidth, height );
//convert the palette to 32bit
unsigned int palette[256];
int index;
for ( index=0; index < 256; index++ )
palette[ index ] = (255<<24) | ( pal[ index*3 ] << 16) | ( pal[ index*3+1 ] << 8) | pal[ index*3+2 ];
//convert the pixels
for ( index=0; index<(wwidth*height); index++)
buffer->pixels[ index ] = palette[ *raw++ ];
return buffer;
anim_image *create_anim_images( gfx_buffer *srce, int wwidth, int height )
{
int frame_w= srce->wwidth / wwidth;
int frame_h= srce->height / height;
int total_frames= frame_w * frame_h;
anim_image *anim_buffer = new anim_image;
anim_buffer->frame=new gfx_buffer *[ total_frames ];
int x,y,frame_number=0;
for ( y=0; y<frame_h*height; y+=height )
{
for ( x=0; x<frame_w*wwidth; x+=wwidth )
{
anim_buffer->frame[ frame_number ]=create_gfx_buffer(wwidth,height);
draw_gfx_buffer( anim_buffer->frame[frame_number],srce,-x,-y);
frame_number+=1;
}
}
anim_buffer->frame_wwidth = frame_w;
anim_buffer->frame_height = frame_h;
anim_buffer->total_frames = total_frames;
return anim_buffer;
}
void draw_gfx_buffer( gfx_buffer *dest, gfx_buffer *srce, int pos_x, int pos_y )
{
unsigned int col=0;
int x_start=pos_x, x_end=pos_x+srce->wwidth-1;
if ( x_start<0 ) x_start=0;
if ( x_end>=dest->wwidth ) x_end=dest->wwidth-1;
if (x_start<=x_end)
{
int y_start=pos_y, y_end=pos_y+srce->height-1;
if (y_start<0) y_start=0;
if (y_end>=dest->height) y_end=dest->height-1;
if (y_start<=y_end)
{
unsigned int *srce_start= srce->pixels+(x_start-pos_x)+(y_start-pos_y) *srce->wwidth;
unsigned int *srce_end = srce->pixels+(x_start-pos_x)+(y_end-pos_y) *srce->wwidth;
unsigned int *srce_ptr;
unsigned int *dest_start= dest->pixels + x_start+y_start*dest->wwidth;
unsigned int *dest_end =dest_start-x_start+x_end;
unsigned int *dest_ptr;
while (srce_start<srce_end)
{
srce_ptr=srce_start;
dest_ptr=dest_start;
while (dest_ptr<dest_end)
{
col=*srce_ptr;
if (col!=0) *dest_ptr=col;
dest_ptr+=1;
srce_ptr+=1;
}
dest_start+=dest->wwidth;
dest_end+=dest->wwidth;
srce_start+=srce->wwidth;
}
}
}
}
I think i've narrowed it down to the create_anim_image routine.
To explain it a bit more, part of the proceeding character / image is present on the actual one being drawn. so it's not formatting them correctly someplace in the creation process.
Cheers,
Clyde.
-
This was working before - which bit have you changed? Have you tried looking at it in the debugger?
Jim
-
Ive not changed any of it which is why im thinking there's gremlins,
I have added a new create_anim_images for bitmap fonts that are a fixed width and height. as the other method was a chopper-esque route.
I think it might be to do with settings either in the C++ list, or the Linker, but im not an expert by any means; when I used the settings to make it smaller exe's with tinyptc & MSVCRTS.lib; im also getting those cpu.obj errors back too.
If think i'll try and see about uninstalling completely MSVC++ 2008 from my PC,, and make backups of stuff i've done so far and restart a fresh. As there's something a miss that I just cant quite explain. As they were working before as you rightly said. And Hell Fire mentioned about code can go a stray.
-
Nooooo!!!!!!!!!!! You've just got it all working!!!!!! The gremlins are in your head!
There's just a bug in your code. Do you reinstall FB every time your code doesn't work?
Zip up your code again and post it here and we'll fix it up. Remember - that's .cpp, .h, .vcproj, .lib, .a files. Not the giant .pdb or .ncb files which are useless to anyone.
Jim
-
Your right mate, they are in my bonce! :D
Ok dude, will do a project to upload and with the suspect font. it might of been subject to being converted incorrect too; but will see if you spot anything a miss in the code first. And could you put me out of my mysery and the gremlins to rest, by showing me when to put the wrapping :D
I have discovered though, that when it draws the letters, black is still being shown. Only spotted that when i drew a frame on top of an effect.
Cheers Amigo,
Clyde.
-
Here's the Gremlins Project - Humble Beginnings.cpp is the main file.
-
my font isnt displaying properly like before, it's got lime green borders around the letters that suggest errors someplace.
I don't have much time to look over your source right now but I don't see any green borders:
(http://www.abload.de/img/screenshotg7bj.png)
Looks like your characters are just off by a pixel or two and the upper font probably has enough margin to hide it.
-
I've got it working now, which is a relief. it was a matter of rechecking the other posts for it, somehow i'd lost them.
the next string commands i'd like to challenge are mid$, left$ and right$.
an example:
left_string( text$, amount_of_characters )
same applies for right string.
left_string( "HELLO WORLD", 5 )
HELLO
right_string( "HELLO WORLD, 5 )
WORLD
i also dont know if in blitz or freebasic, it wraps if it goes out of bounds.
Cheers all,
Clyde.
-
untested and with a few glasses of wine:
char* left(char *src, int count)
{
int len= strlen(src); // length of source string
if (count>len) count= len; // don't copy more than available
char *dst= new char[count+1]; // allocate memory for new string
memcpy(dst, src, count); // copy string from the beginning
dst[count]= 0; // store 0-terminator at end of string
return dst; // return new string
}
char* right(char *src, int count)
{
int len= strlen(src);
if (count>len) count= len;
char *dst= new char[count+1];
memcpy(dst, src+len-count, count);
dst[count]= 0;
return dst;
}
keep in mind that you have to delete the resulting string manually:
char *text= "hello world";
char *front= left(text, 5);
char *back= right(text, 5);
delete[] front;
delete[] back;
a better option is to use a proper string-class like std::string (http://www.cplusplus.com/reference/string/string).
Personally I prefer the one coming with Qt (http://qt.nokia.com/doc/4.5/qstring.html) but that's probably a bit much for now.
-
Corker mate, will give this a go in a bit :D
-
Wonder how to incorpate a tag system, for example: hello eddie[pause 2000] nice to see you to see you [bounce 1,2,3] nice! [pause]
-
Wonder how to incorpate a tag system, for example: hello eddie[pause 2000] nice to see you to see you [bounce 1,2,3] nice! [pause]
Have your code check to see if the next character is [ then read on and collect characters until you meet a ] after which you decide what to do with the command you have collected.
-
im hopeless it crashes, and not sure.
void update_bitmap_scroller( gfx_buffer *dest, sint pos_y )
{
int curchar= 0;
int pos_x=(int) start_pos;
// find first character that's not outside the left border
if ( display==0 )
{
while ( message[curchar] && pos_x+font->frame[ message[curchar] - 32]->wwidth<0)
{
pos_x+=font->frame[ message[ curchar ] - 32]->wwidth;
curchar++;
}
}
if ( message[ curchar ]=='[' )
{
display=1;
command_script+=message[ curchar ];
if (message[curchar]==']')
{
if ( command_script=="PAUSE 1")
{
pause_mode=1;
command_script="";
}
if ( command_script="PAUSE 2" )
{
pause_mode=2;
command_script="";
}
}
//curchar+=1;
}
// wrap master 3000.
if ( curchar >= message_length)
{
start_pos=0.0f;
}
// draw characters until right border reached
while (pos_x<dest->wwidth && message[curchar])
{
char c=message[curchar] - 32;
draw_gfx_buffer( dest, font->frame[c], pos_x, pos_y );
pos_x+=font>frame[c]->wwidth;
curchar++;
}
start_pos-=scroll_speed;
}
-
This is a comparison:
if (command_script=="PAUSE 1")
This is an assignment:
if (command_script="PAUSE 2" )
What data-type is "command_script" ?
-
Clyde, in C++ strings don't work like they do in BASIC.
So
a = "X"
b = "X"
basic:
if a=b then print "YES"
will print YES
C:
if (a==b) printf("YES");
will not print anything.
The reason being it's comparing the address of a with the address of b, not the strings that they point to.
basic:
c=a+b
print c
will print XX
C:
c=a+b
won't even compile
This line
command_script+=message[ curchar ];
almost certainly doesn't do what you want it to do. You are wanting to make a string in command_script by adding letters to the end. That's not how strings work in C. strings in C are just arrays of characters. You might get some of what you want using C++'s 'string' class.
As Hellfire points out, it's really important not to get = and == mixed up.
Jim
-
Ok thanks!
command_script is the same as message:
static char *message1;
static char *command_script;
just dawned on me: command_script[index]=message[current_char];
edit - dont try this at home either.
void update_bitmap_scroller( gfx_buffer *dest, anim_image *font, int pos_y )
{
static int command_index, execute;
int curchar= 0;
int pos_x=(int) start_pos;
// find first character that's not outside the left border
if ( display=0 )
{
while ( message[curchar] && pos_x+font->frame[ message[curchar] - 32]->wwidth<0)
{
pos_x+=font->frame[ message[ curchar ] - 32]->wwidth;
curchar++;
}
}
if ( message[ curchar ]='[' )
{
display=1;
command_script[command_index]=scroller->message[ curchar ];
if (scroller->message[curchar]=']')
{
execute=1;
}
}
if ( execute=1 )
{
if ( command_script[0]='W' && command_script[1]='A' && command_script[2]='I' && command_script[3]='T' )
{
pause=1;
}
}
// wrap master 3000.
if ( curchar >= scroller_length)
{
start_pos=0.0f;
}
// draw characters until right border reached
while (pos_x<dest->wwidth && message[curchar])
{
char c= message[curchar] - 32;
draw_gfx_buffer( dest, scroller->font->frame[c], pos_x, pos_y );
pos_x+=font->frame[c]->wwidth;
if ( pause!=0 ) curchar++;
}
start_pos-=scroll_speed;
}
-
OK, but now you are writing to memory you don't own.
char *command_script;is just thing that can hold a pointer. Right now it's blank (or some random garbage) so it's pointing at nothing in particular. You might get away with it just working, by not overwriting anything important, but one day it will cause a crash.
You need to initialise it to something, something big enough to hold your longest command strings, like this:
char *command_script = new char [50];
That will point it at 50 characters, but you have to remember to free it up at the end, like this
delete [] command_script;
You could instead declare
char command_script[50];
which gives you 50 chars of space (initially filled with garbage).
New problem: You've used = instead of == in the new code.
if ( execute=1 )
is always true, because it's an assignment. It puts the value 1 into execute and then does if (1) which is always true.
You want
if ( execute==1 )
When are you updating command_index? Seems it will always be 0.
This is always true too
if ( command_script[0]='W' && command_script[1]='A' && command_script[2]='I' && command_script[3]='T' )
should be
if ( command_script[0]=='W' && command_script[1]=='A' && command_script[2]=='I' && command_script[3]=='T' )
This is long winded way of checking for strings though. If you can arrange for command_script[4] to be 0 (perhaps when you see the ']' character you could stick it on the end then) then instead you can use
if (strcmp(command_script, "WAIT")==0)
You need #include <string.h> to use that.
Jim
-
I thought I had this on the 4th attempt, It crashes when it gets to a tag / command. Could you have a peek and help me fix it once and for all ;)
Cheers and big thanks,
Clyde.
-
interesting and i would love to help ya but to honest..I leave to expert such as jim and hellfire to help ya :)
Keep going Clyde :) You are getting there ;D You will be overjoy when it is finish :)
-
The reason why your code crashes is that your scrolltext contains a "[" character which is not available in your font.
When you're trying to draw it in update_scroller using draw_gfx_buffer, the data in "scroll->font->frame[ (int)frame ]" is not properly initialized.
I'd suggest to create enough space for all possible characters and initialize the unused ones to an empty "gfx_buffer".
This bug was rather easy to find:
Just build as "debug" and run with debugger (F5).
It drops out in draw_gfx_buffer where "srce" points to some odd adress and its' content is garbage.
In the call stack you can see that draw_gfx_buffer was called from update_scroller where "scroll->message[letter_pos]" is '['.
Without the debugger you can spend hours to find such simple bugs. You should really try it!
Another, more general, thing is you shouldn't do things like that:
screen_buffer= graphics("bitMap scRo77eR TAgS", "", 640,480);
...
gfx_buffer *graphics( char *title, char *title2, int wwidth, int height )
{
if (title2=="") title2="would you prefer full screen dude?";
You're comparing two char* which are adresses to some location in memory (so they're just numbers).
As the compiler is clever enough to create only a single string when it occurs multiple times, your two occurrences of "" actually point to the same adress in memory - so this code works but that's merely accidental :)
If you want to handle empty strings use 0:
screen_buffer= graphics("bitMap scRo77eR TAgS", 0, 640,480);
gfx_buffer *graphics( char *title, char *title2, int wwidth, int height )
{
if (title2==0) title2="would you prefer full screen dude?";
-
thanks hellfire :)
wasnt aware of the 0. I also forgot about checking against the total frames.
the tag detection / scroller isnt working as intended; I tried it with different symbols (pause), and it still displayed that in the message. so will need to revisit a new one of this in the future.