How Can I Make a Double Buffer? I Made a Bitmap Loader, Help please !!!!!

Jun 25, 2011 at 1:57pm
Hi ! Im Spanish. My English is low. I'm sorry.

I made a BitMap Loader in Turbo C++ 3.0. I have a old computer for learn C++.

I finished my bitmap loader, but I have a problem. My bitmap loader is slow.

When the Bitmap loader is loading , I can see it. The screen is loading slowly.

I read in internet: I need double buffer , I need make a buffer for save the data there, and later, move the data to video memory, VRAM, with memcpy().

I read, I must use calloc(), or INT 21 ( DOS SERVICES ) Function 48H, but when I use Function 48H, it say : No enought memory. Need more memory. I have 32 MB Memory . Is enought I need only 64 kb to double buffer.

You must make a bmp file and save it in 256 colors. Open Paint, draw, save as, 256 colors. Save the file in C:\, name file : nuevo1.bmp. Perhaps you can't see all colors, but isn't problem. The code is OK, is perfect. The problem is Microsoft Paint.

Here is my code , you can try it in Dev C++ 4.9.9.2 and Turbo C++ 3.0 . Turbo C++ is better. The code was written in Turbo C++ 3.0.


Please, How Can I make a double buffer? . Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

//BITMAP LOADER BY TSW 2011.
//PROGRAMADOR : Gabriel Gonz lez Gonz lez
//E-MAIL : gabrielgg85@gmail.com
//AGRADECIMIENTOS A MIGUEL Y A BOB (Canad ) POR SU AYUDA
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <graphics.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>   //LIBRERIAS
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <math.h>

informacion(); //subprograma cabecera

main()
{
//SUBPROGRAMA INFORMACION

	clrscr();
	cout << "BITMAP LOADER 1.0 BY TSW\n";

	char *fil = "c:\\nuevo1.bmp";
	FILE *archivo;
	archivo = fopen(fil,"rb"); //binario

	if(archivo == NULL )
	printf("ERROR GRAVE !!! NO SE HA PODIDO ABRIR EL ARCHIVO!!!\n");
	if (archivo)
		printf("ARCHIVO ABIERTO CON XITO !!!\n");

cout << "Ruta de Archivo " << fil ;
cout << "\n---------------------------------------\n";
cout << "INFORMACIàN DE CABECERA ARCHIVO BMP \n";
cout << "---------------------------------------\n";

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

long tamano;
long offset;
long ancho;       //nombro variables
long alto;
int planos;
int bpp;
long compresion;
long tamanoBmpPixel;
long NumColores;
char tipo[1]; //Tipo BMP ( Microsoft,OS/2...)

tamano = 0;
offset = 0;
ancho = 0;       //inicializo variables para que no tomen valores extra¤os
alto= 0;
planos= 0;
bpp= 0;
compresion= 0;
tamanoBmpPixel= 0;
NumColores= 0;


fseek(archivo,0L,SEEK_SET); //Tipo de bmp
   fread(tipo,1,2,archivo);
   printf("þ     Tipo de BMP   = %s \n",tipo);
//   rewind(archivo);

fseek(archivo,2L,SEEK_SET); //---tama¤o archivo bmp
     fread(&tamano,1,4,archivo);
     printf("þ     Tama¤o de BMP = %lu Bytes\n",tamano);

fseek(archivo,10L,SEEK_SET); //----offset archivo
     fread(&offset,1,4,archivo);
     printf("þ     Offset BMP    = %d (Datos im gen empiezan en este byte)\n",offset);


fseek(archivo,18L,SEEK_SET); //----ancho archivo
    fread(&ancho,1,4,archivo); //guardo en &ancho, 1 registro de 4 bytes.
    printf("þ     Ancho de BMP  = %d\n",ancho);

fseek(archivo,22L,SEEK_SET); //----alto bmp
    fread(&alto,1,4,archivo);
    printf("þ     Alto de BMP   = %d\n",alto);

fseek(archivo,26L,SEEK_SET); //----numero de planos
    fread(&planos,1,2,archivo);
    printf("þ     Numero de planos BMP = %d\n",planos);

fseek(archivo,28L,SEEK_SET); //----bit por pixel ( 256 colores 8 bit)
    fread(&bpp,1,2,archivo);
    printf("þ     Bit por pixel (BPP) = %d\n",bpp);

fseek(archivo,30L,SEEK_SET); //----compresion archivo bmp
    fread(&compresion,1,2,archivo);
    printf("þ     ¨Existe compresi¢n? (0->NO) = %d\n",compresion);

fseek(archivo,34L,SEEK_SET); //---cantidad en pixel
    fread(&tamanoBmpPixel,1,4,archivo);
    printf("þ     Tama¤o im gen %lu pixeles\n",tamanoBmpPixel);

fseek(archivo,46L,SEEK_SET); //--- numero colores bmp
    fread(&NumColores,1,4,archivo);
    printf("þ     N£mero de colores de im gen %d\n",NumColores);

cout << "\n";
cout << "PULSE UNA TECLA PARA MOSTRAR EL BMP EN PANTALLA";
getch();

long  rojo,verde,azul,contador;
long  dato,cuenta_colores;
long  valorLetra;
int   valor;    //con INT SALE MAS COLOR original CHAR
int r,v,a,c;
r = 0; v= 0; a= 0; c= 0;
valor = 0;
valorLetra = 0;
cuenta_colores = 0;
contador  =  0;
dato      =  0;

	asm{
	  mov ah,0
	  mov al,13h  //mode 320x200x256
	  int 10h
	}


	fseek(archivo,54L,SEEK_SET); //--- numero colores bmp
	do
	   {

		outportb(0x3c8,cuenta_colores);
		fread(&valor,1,1,archivo);
		   valorLetra = valor;
		   azul = abs(valorLetra /4);   //falta poner valor ascii
		   valorLetra = 0;
		   a = azul;

		fread(&valor,1,1,archivo);
		  valorLetra = valor;
		  verde = abs(valorLetra/4 );
		  valorLetra = 0;
		  v = verde;

		fread(&valor,1,1,archivo);
		   valorLetra = valor;
		   rojo = abs(valorLetra/4 );
		   valorLetra = 0;
		   r = rojo;

		fread(&valor,1,1,archivo);
		valorLetra = 0;
		cuenta_colores = cuenta_colores + 1;

		 outportb(0x3c9,r);
		 outportb(0x3c9,v);
		 outportb(0x3c9,a);
	    }

	while(cuenta_colores <= 255);
	getch();

	do
	    {
		fseek(archivo,1078L+contador,SEEK_SET);
		fread(&dato,1,1,archivo);
		pokeb(0xa000,contador,dato);   // Here I want the bouble buffer
		contador = contador + 1 ;      // How?¿?¿ . The load is slow.
	    }
	while (contador <=63779);

	getch();

     asm{
	mov ah,0
	mov al,02h
	int 10h
	}

return(0);
}




Jun 28, 2011 at 2:51am
Hi, you should update your compiler and your reference book. main must return int, and informacion must have a return type.

1
2
3
4
5
6
7
8
	do
	    {
		fseek(archivo,1078L+contador,SEEK_SET); //¿isn't this update automatic?
		fread(&dato,1,1,archivo);  //reading just 1 byte
		pokeb(0xa000,contador,dato);
		contador = contador + 1 ;
	    }
	while (contador <=63779); //¿what is that magic number? 
Just read all the junk at one time
1
2
char buffer[size_of_the_data];
fread(buffer, size_of_the_data, sizeof(*buffer), file);
Then pass the buffer to pokeb (don't know about this function). Also erase all your fseek the pointer is already there.

Your code is system specific (you even put assembler), however I dunno how it could be done portable. ¿don't you want to use a library?

Another things
1
2
3
4
5
6
7
//char tipo[1];  //just one character
//fread(tipo,1,2,archivo); //2 is too much, out of bounds

int valor;
fread(&valor,1,1,archivo); //dangerous, you are depending on how the ints are stored, and its dimension
//you better make valor a char or an unsigned char.
valorLetra = valor;
Your code is (invalid) C, not C++
Topic archived. No new replies allowed.