#include <cstdio>
#include <unistd.h>
#include <SDL/SDL.h>
#define COL(x, y) (bool)*((int*)screen->pixels + (screen->w * y * 4) + (x * 4))
#define RESULT_SIZE 1260000
#define WIDTH 110
#define HEIGHT 120
int main(){
SDL_Event event;
printf("[....] Initialize SDL\r");
SDL_Init(SDL_INIT_VIDEO);
printf("[ ok ] Initialize SDL\n");
printf("[....] Initialize Window\r");
SDL_Surface* screen = SDL_SetVideoMode(110, 120, 32, SDL_HWSURFACE);
printf("[ ok ] Initialize Window\n");
printf("[....] Load font bmp\r");
SDL_Surface* image = SDL_LoadBMP("./font.bmp");
printf("[ ok ] Load font bmp\n");
printf("[....] Draw\r");
SDL_BlitSurface(image, NULL, screen, NULL);
SDL_Flip(screen);
printf("[ ok ] Draw\n");
//Export 10*10 fonts to file
printf("[info] Export as binary file...\n");
printf("[....] Open result file...\r");
FILE* out = fopen("font.pb", "w");
if(out == NULL){
perror("[FAIL] Open result file... Failed!");
return 1;
}
printf("[ ok ] Open result file...\n");
printf("Writing... Please wait...\n");
printf("______________________________\r");
int write = 0;
for(int startY = 0; startY < HEIGHT; startY += 10)
for(int startX = 0; startX < WIDTH; startX += 10)
for(int x = startX; x < startX + 10; x++){
int y;
for(y = startY; y < startY + 10; y++){
float p = (((float)write/(float)RESULT_SIZE)*100)/2;
int bar = (int) p;
for(int i = 0; i < 50; i++) if(i < p) printf("="); else printf("_");
if(write < 1024)
printf(" %dB/1230K\r", write);
else{
float kb = write / 1024;
printf(" %dK/1230K\r", (int) kb);
}
printf("sx: %d sy: %d x: %d y: %d c: %d---", startX, startY, x, y, COL(x, y));
fputc(COL(x, y), out);
printf("ok\n");
write++;
}
y = 0;
}
fclose(out);
printf("\nDone.");
}
这是源代码。该程序使用SDL加载位图文件。 在位图中,有10 * 10种字体。
加载位图时,会将每个10 * 10像素复制到文件中。 (像这样: 位图: BBBBBBBBBBWWW .... BWBWBWBWBW BWBWBWBWBW BWBWBWBWBW BWBWBWBWBW BWBWBWBWBW BWBWBWBWBW BWBWBWBWBW BWBWBWBWBW BWBWBWBWBW
文件: 11111111111010101010 ..... 1010111 )
但该计划的结果是......
... sx: 50 sy: 30 x: 59 y: 35 c: 0---ok_______________ 3K/1230K sx: 50 sy: 30 x: 59 y: 36 c: 1---ok_______________ 3K/1230K sx: 50 sy: 30 x: 59 y: 37 c: 1---ok_______________ 3K/1230K sx: 50 sy: 30 x: 59 y: 38 c: 0---ok_______________ 3K/1230K sx: 50 sy: 30 x: 59 y: 39 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 30 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 31 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 32 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 33 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 34 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 35 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 36 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 37 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 38 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 60 y: 39 c: 1---ok_______________ 3K/1230K sx: 60 sy: 30 x: 61 y: 30 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 61 y: 31 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 61 y: 32 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 61 y: 33 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 61 y: 34 c: 0---ok_______________ 3K/1230K sx: 60 sy: 30 x: 61 y: 35 c: 0---ok_______________ 3K/1230K ... sx: 100 sy: 30 x: 108 y: 34 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 108 y: 35 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 108 y: 36 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 108 y: 37 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 108 y: 38 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 108 y: 39 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 30 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 31 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 32 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 33 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 34 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 35 c: 1---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 36 c: 1---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 37 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 38 c: 1---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 39 c: 0---ok_____________ 4K/1230K sx: 0 sy: 40 x: 0 y: 40 c: 0---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 41 c: 1---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 42 c: 1---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 43 c: 1---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 44 c: 0---ok_________________ 4K/1230K **Segmentation fault (core dumped)** next line maybe..(It couldn't shown because of error) sx: 0 sy: 40 x: 0 y: 45 c: 0---ok_________________ 4K/1230K
在此错误之前,它运行良好。(显示BMP文件) 我认为错误是由
引起的(bool)((int )screen-&gt; pixels +(screen-&gt; w * y * 4)+(x * 4)) **编辑:“* 4”是......每个像素是4个字节。所以对于正确的地址,需要 4 *
我还检查了gdb。它使用它停止在线。 我检查了内存地址,但事实并非如此。 X和Y是正确的,它在窗口内。
为什么停止了?
修改
我在g ++上尝试了-g选项(用于调试) 它的结果是
sx: 100 sy: 30 x: 109 y: 35 c: 1---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 36 c: 1---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 37 c: 0---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 38 c: 1---ok_____________ 4K/1230K sx: 100 sy: 30 x: 109 y: 39 c: 1---ok_____________ 4K/1230K sx: 0 sy: 40 x: 0 y: 40 c: 1---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 41 c: 0---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 42 c: 1---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 43 c: 0---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 44 c: 1---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 45 c: 0---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 46 c: 0---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 47 c: 0---ok_________________ 4K/1230K sx: 0 sy: 40 x: 0 y: 48 c: 0---ok_________________ 4K/1230K Program received signal SIGSEGV, Segmentation fault. 0x0000000000400def in main () at bitmap.cpp:61 warning: Source file is more recent than executable. 61 printf("sx: %d sy: %d x: %d y: %d c: %d---", startX, startY, x, y, COL(x, y)); (gdb) bt #0 0x0000000000400def in main () at bitmap.cpp:61 (gdb)
如您所见,错误是由使用获取Pixel引起的。(COL(x,y))