Thanks Morcilla, I think I get what you say, then makes me wonder why it is still crashing here, I mean, well you saw my test code, its really simple stuff, and makes the program crash badly, also I added your code to empty DarkGDK template and the program crashes too, could I have something wrong in my installation? I see not many other explanations to this, I set your code like this to be more exact:
#include "DarkGDK.h"
char *file_name;
int value_read[256];
FILE *my_stream;
FILE *my_stream2; //clone to know if a new line char is read "\n"
int ch;
int null_int;
char read_buffer[256];
int charRead=0;
int line=0;
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
/* Open file to read line from: */
my_stream = fopen( file_name, "r" );
my_stream2 = fopen( file_name, "r" );
//aux read pointer
charRead=0; //charRead
line = 0;
//While not End Of File
while ( (ch = fgetc(my_stream2)) != EOF )
{
read_buffer[charRead]=fgetc( my_stream );
charRead++;
while ( ch != '\n' ) // While not new line
{
//move pointer stream2
ch=fgetc( my_stream2 );
//if not new line, read and store value
if ( ch != '\n' )
{
read_buffer[charRead]=fgetc( my_stream );
}
else
{
//if new line character is read, discard it and go for the next line
null_int = fgetc( my_stream );
}
charRead++;
if (charRead>255) break; //array overflow security
}
//Terminate string security
strcat(read_buffer,"\0");
//store integer values
value_read[line] = atoi( read_buffer );
charRead=0;
line++;
if (line>255) break; //array overflow security
}
fclose( my_stream );
fclose( my_stream2 );
while ( LoopGDK ( ) )
{
dbSync ( );
}
return;
}
Also I can provide the file input that I need to read and the operations I need to perform if you think it will be better, but its more complex, anyway here its my current code for that solution I mentioned I was trying to accomplish:
(This code is suppossed to access DAT file, get each pixel information, plot it into screen and when done save the screen painted section (image) to BMP firmat directly from screen)
#include "DarkGDK.h"
int sx,sy;
int widthh;
FILE *fp;
char p[3]={0};
int t = 128;
int v;
char res[17];
char Redd[5];
char Greenn[5];
char Bluee[5];
int Alphaa;
int RedF;
int GreenF;
int BlueF;
int read2byes();
int paintpixel(int xx, int yy, int RR, int GG, int BB);
int bintodec(char *bin);
int falseAlpha;
int stillreading;
float convnumber=8.2258f;
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
sx=0;
sy=0;
widthh=640;
falseAlpha=dbRGB(5,5,5);
fp=fopen("startbar.dat","rb");
stillreading=1;
while ( LoopGDK ( ) )
{
while(stillreading)
{
for(sx=0;sx<widthh;sx++)
{
stillreading=read2byes();
}
sy+=1;
}
dbSync ( );
dbGetImage(1,0,0,widthh,sy);
dbSaveImage("testasd.bmp",1);
break;
}
// return back to windows
return;
}
int read2byes()
{
int retval;
int indexv=0;
t=128;
retval=fscanf(fp,"%c%c",p[0],p[1]);
if(retval!=EOF)
{
v = strtol(p, 0, 16);
while(t)
{
if(t<v)
{
v-=t;
res[indexv]='1';
}
else
{
res[indexv]='0';
}
t /= 2;
indexv+=1;
}
res[16] = '\0';
Greenn[2]=res[0];
Greenn[3]=res[1];
Greenn[4]=res[2];
Bluee[0]=res[3];
Bluee[1]=res[4];
Bluee[2]=res[5];
Bluee[3]=res[6];
Bluee[4]=res[7];
if(res[8]=='1')
{
Alphaa=1;
}
else
{
Alphaa=0;
}
Redd[0]=res[9];
Redd[1]=res[10];
Redd[2]=res[11];
Redd[3]=res[12];
Redd[4]=res[13];
Greenn[0]=res[14];
Greenn[1]=res[15];
//[G3][G4][G5][B1][B2][B3][B4][B5][A][R1][R2][R3][R4][R5][G1][G2]
RedF=bintodec(Redd);
GreenF=bintodec(Greenn);
BlueF=bintodec(Bluee);
dbDot(sx,sy,dbRGB(floor(RedF*convnumber),floor(GreenF*convnumber),floor(BlueF*convnumber)));
return 1;
}
fclose(fp);
return 0;
}
int paintpixel(int xx, int yy,int RR, int GG, int BB)
{
int ccolor;
ccolor=dbRGB(RR,GG,BB);
dbDot(xx,yy,ccolor);
return 0;
}
int bintodec(char *bin)
{
int dec = 0;
int i;
for(i=4;i>=0;i--)
{
if (bin[i] == '1')
{
dec = dec +2^i;
}
}
return dec;
}
The test file is here:
http://www.mediafire.com/file/mo9g34cepzaj2a1/startbar.dat
The code is a bit messed Im sorry I went a little crazy when it started to crash over and over and later I realized it was just file input that was making it crash, the file Im accesing is this one, its Revenant (medieval game) graphic file, it contains a image but stored in a very special format that goes like this, well its little bit hard to explain, but the file is stored using hexadecimal data, and each time we read 2 bytes it can be converted to binary format and then from this binary string we can get the colors for one pixel, a friend told me an example, its better seen this way:
1) We read first 2 bytes (16 bits) from file. It's two hex values, for example E0 F0.
2) We convert them into binary digits. It's 11100000 and 11110000
3) As a single line it's 1110000011110000. Decoding formula is [G3][G4][G5][B1][B2][B3][B4][B5][A][R1][R2][R3][R4][R5][G1][G2].
So,
red is 11100 (binary) or 28 (decimal) or 230 (color as 0-255)
blue is 00000 (binary) or 0 (decimal) or 0 (color as 0-255)
green is 00111 (binary) or 7 (decimal) or 58 (color as 0-255)
alpha is 1
So, result is (230,0,58) or simply pink
Well of course my main goal is to achieve that file interpretation to convert these DAT files into BMP and viceversa, but I think as soon as file input works it should work the same for file conversion code. Thanks a lot for your help this was driving me crazy.
Edit: Well as I said the bigger code isnt tested so these functions to convert into binary or so could be wrong at some point, the issue arrived with file input so I couldnt test alone these functions yet, just in case you find horrible operations in this functions