int main(int argc, char* argv[]) { PFont pf[2] ; int i; NGE_Init(INIT_VIDEO); //NGE_SetFontEncoding(NGE_ENCODING_UTF_8); InitInput(btn_down,btn_up,1); int maxid = GetInfoCount(); //创建一个显示image,字就显示在这个上面注意DISPLAY_PIXEL_FORMAT必须与创建字体的DISPLAY_PIXEL_FORMAT一致 pimage_text = image_create(512,512,DISPLAY_PIXEL_FORMAT_4444); //创建字体 pf[0] = create_font_hzk("fonts/GBK14","fonts/ASC14",14,DISPLAY_PIXEL_FORMAT_4444); pf[1] = create_font_freetype("fonts/simfang.ttf",13,DISPLAY_PIXEL_FORMAT_4444); char str[3][128]={"【小萝莉】","众芳摇落独暄妍,占尽风情向小园。","疏影横斜水清浅,暗香浮动月黄昏。"}; //显示GBK Font font_setcolor(pf[0],MAKE_RGBA_4444(128,0,0,255)); font_drawtext(pf[0],str[0],strlen(str[0]),pimage_text,100,195,FONT_SHOW_NORMAL); NGE_SetFontEncoding(NGE_ENCODING_GBK); for(i = 0;i<maxid;i++){ font_drawtext(pf[0],CreateInfoByid(i),strlen(CreateInfoByid(i)),pimage_text,120,200+i*20,FONT_SHOW_SHADOW); font_setcolor(pf[0],MAKE_RGBA_4444(255,0,0,255)); } //显示freetype font_setcolor(pf[1],MAKE_RGBA_4444(128,0,0,255)); NGE_SetFontEncoding(NGE_ENCODING_UTF_8); font_drawtext(pf[1],str[0],strlen(str[0]),pimage_text,100,30,FONT_SHOW_NORMAL); //for(i =1;i<3;i++){ // font_drawtext(pf[1],str[i],strlen(str[i]),pimage_text,120,35+i*20,FONT_SHOW_NORMAL); // font_setcolor(pf[1],MAKE_RGBA_4444(255,0,0,255)); //} pimage_bg = image_load("images/demo1_bg.jpg",DISPLAY_PIXEL_FORMAT_8888,1); if(pimage_bg == NULL) { printf("can not open file\n"); } pimage_box = image_load("images/demo1_box.jpg",DISPLAY_PIXEL_FORMAT_8888,1); if(pimage_box == NULL) { printf("can not open file\n"); } pimage_icon[0] = image_load_colorkey("images/demo1_icon0.bmp",DISPLAY_PIXEL_FORMAT_8888,MAKE_RGB(0,0,0),1); pimage_icon[1] = image_load_colorkey("images/demo1_icon1.png",DISPLAY_PIXEL_FORMAT_8888,MAKE_RGB(0,0,0),1); while ( !game_quit ) { ShowFps(); InputProc(); DrawScene(); } font_destory(pf[0]); font_destory(pf[1]); image_free(pimage_bg); image_free(pimage_text); image_free(pimage_box); NGE_Quit(); return 0; }
uint32_t freetype2_setfontcolor(PFont pfont, uint32_t color) { PFontFreetype pf = (PFontFreetype) pfont; uint32_t last_color; switch(pf->disp) { case DISPLAY_PIXEL_FORMAT_5551: last_color = MAKE_RGBA_5551(pf->r,pf->g,pf->b,pf->a); GET_RGBA_5551(color,pf->r,pf->g,pf->b,pf->a); break; case DISPLAY_PIXEL_FORMAT_4444: last_color = MAKE_RGBA_4444(pf->r,pf->g,pf->b,pf->a); GET_RGBA_4444(color,pf->r,pf->g,pf->b,pf->a); break; case DISPLAY_PIXEL_FORMAT_565: last_color = MAKE_RGBA_565(pf->r,pf->g,pf->b,pf->a); GET_RGBA_565(color,pf->r,pf->g,pf->b,pf->a); break; // case DISPLAY_PIXEL_FORMAT_8888: default: last_color = MAKE_RGBA_8888(pf->r,pf->g,pf->b,pf->a); GET_RGBA_8888(color,pf->r,pf->g,pf->b,pf->a); break; } freetype2_calcalphatable(pfont); return last_color; }
void scale_col (uint8 *sdata, uint32 sw, uint8 *ddata, uint32 dw,uint32 dh, uint32 u, line_contrib_type *contrib,uint32 dpitch,uint32 dtype) { uint32 y, i; uint8 r,g,b,a,sr,sb,sg,sa; uint32 left,right; uint32 *src32,*dst32; uint16 *src16,*dst16; src32 = (uint32*)sdata; dst32 = (uint32*)ddata; src16 = (uint16*)sdata; dst16 = (uint16*)ddata; for (y = 0; y < dh; y++) { // Loop through column left = contrib->contrib_row[y].left; // Retrieve left boundries right = contrib->contrib_row[y].right; // Retrieve right boundries r = 0,g = 0,b = 0,a = 0; for (i = left; i <= right; i++) { // Scan between boundries // Accumulate weighted effect of each neighboring pixel if(dtype == DISPLAY_PIXEL_FORMAT_8888){ GET_RGBA_8888(src32[i * sw + u],sr,sg,sb,sa); } else if(dtype == DISPLAY_PIXEL_FORMAT_4444){ GET_RGBA_4444(src16[i * sw + u],sr,sg,sb,sa); } else if(dtype == DISPLAY_PIXEL_FORMAT_5551){ GET_RGBA_5551(src16[i * sw + u],sr,sg,sb,sa); } else if(dtype == DISPLAY_PIXEL_FORMAT_565){ GET_RGBA_565(src16[i * sw + u],sr,sg,sb,sa); } if(sa==0) continue; r += (uint8)(contrib->contrib_row[y].weights[i-left] * (double)(sr)); g += (uint8)(contrib->contrib_row[y].weights[i-left] * (double)(sg)); b += (uint8)(contrib->contrib_row[y].weights[i-left] * (double)(sb)); a += (uint8)(contrib->contrib_row[y].weights[i-left] * (double)(sa)); } if(dtype == DISPLAY_PIXEL_FORMAT_8888) dst32[y*dpitch+u] = MAKE_RGBA_8888(r,g,b,a); // Place result in destination pixel else if(dtype == DISPLAY_PIXEL_FORMAT_4444) dst16[y*dpitch+u] = MAKE_RGBA_4444(r,g,b,a); // Place result in destination pixel else if(dtype == DISPLAY_PIXEL_FORMAT_5551) dst16[y*dpitch+u] = MAKE_RGBA_5551(r,g,b,a); // Place result in destination pixel else if(dtype == DISPLAY_PIXEL_FORMAT_565) dst16[y*dpitch+u] = MAKE_RGBA_565(r,g,b,a); // Place result in destination pixel } }
void scale_row (uint8 *sdata, uint32 sw,uint8 *ddata, uint32 dw, uint32 u, line_contrib_type *contrib,uint32 spitch,uint32 dtype) { uint32 x, i; uint32 *src32, *dst32; uint16 *src16, *dst16; uint8 sr, sg, sb, sa,r = 0,g = 0,b = 0,a = 0; src32 = (uint32*)sdata+u*spitch; dst32 = (uint32*)ddata+u*dw; src16 = (uint16*)sdata+u*spitch; dst16 = (uint16*)ddata+u*dw; for (x = 0; x < dw; x++) { // Loop through row int left = contrib->contrib_row[x].left; // Retrieve left boundries int right = contrib->contrib_row[x].right; // Retrieve right boundries r = 0,g = 0,b = 0,a = 0; for (i = left; i <= right; i++) { // Scan between boundries // Accumulate weighted effect of each neighboring pixel if(dtype == DISPLAY_PIXEL_FORMAT_8888){ GET_RGBA_8888(src32[i],sr,sg,sb,sa); } else if(dtype == DISPLAY_PIXEL_FORMAT_4444){ GET_RGBA_4444(src16[i],sr,sg,sb,sa); } else if(dtype == DISPLAY_PIXEL_FORMAT_5551){ GET_RGBA_5551(src16[i],sr,sg,sb,sa); } else{ GET_RGBA_565(src16[i],sr,sg,sb,sa); } if(sa==0) continue; r += (uint8)(contrib->contrib_row[x].weights[i-left] * (double)(sr)); g += (uint8)(contrib->contrib_row[x].weights[i-left] * (double)(sg)); b += (uint8)(contrib->contrib_row[x].weights[i-left] * (double)(sb)); a += (uint8)(contrib->contrib_row[x].weights[i-left] * (double)(sa)); } if(dtype == DISPLAY_PIXEL_FORMAT_8888) dst32[x] = MAKE_RGBA_8888(r,g,b,a); // Place result in destination pixel else if(dtype == DISPLAY_PIXEL_FORMAT_4444) dst16[x] = MAKE_RGBA_4444(r,g,b,a); else if(dtype == DISPLAY_PIXEL_FORMAT_5551) dst16[x] = MAKE_RGBA_5551(r,g,b,a); else if(dtype == DISPLAY_PIXEL_FORMAT_565) dst16[x] = MAKE_RGBA_565(r,g,b,a); } }
image_p image_load_jpg_buf(const char* mbuf,int bsize, int displaymode) { image_p pimage = NULL; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; uint8 *rawdata,*scanline, *p,*data = NULL; int rawsize,texw,texh,width,height,bpb=2,size; int r,g,b,x; uint32* p32; uint16* p16; uint16 color16; uint32 color32; if(mbuf == NULL||bsize==0) return 0; memset(&cinfo,0,sizeof(struct jpeg_decompress_struct)); memset(&jerr,0,sizeof(struct jpeg_error_mgr)); rawsize = bsize; rawdata = (uint8*)mbuf; if (rawdata[6] != 'J' || rawdata[7] != 'F' || rawdata[8] != 'I' || rawdata[9] != 'F') { return 0; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_mem_src(&cinfo, rawdata, rawsize); jpeg_read_header(&cinfo, 1); jpeg_start_decompress(&cinfo); if(cinfo.output_components != 3 && cinfo.output_components != 4) { jpeg_destroy_decompress(&cinfo); return 0; } texw = roundpower2(cinfo.output_width); texh = roundpower2(cinfo.output_height); width = cinfo.output_width; height = cinfo.output_height; if(displaymode == DISPLAY_PIXEL_FORMAT_8888){ bpb = 4; } size = texw * texh * bpb; data = (uint8*)malloc(size); memset(data,0,size); scanline = (uint8*)malloc(cinfo.output_width * 3); if(!scanline){ jpeg_destroy_decompress(&cinfo); SAFE_FREE (data); return 0; } p32 = (uint32*)data; p16 = (uint16*) p32; while(cinfo.output_scanline < cinfo.output_height){ jpeg_read_scanlines(&cinfo, &scanline, 1); p = (uint8*)scanline; for(x=0; x<(int)cinfo.output_width; x++){ r = p[0]; g = p[1]; b = p[2]; switch (displaymode){ case DISPLAY_PIXEL_FORMAT_565: color16 = MAKE_RGBA_565(r,g,b,0xff); *(p16+x) = color16; break; case DISPLAY_PIXEL_FORMAT_5551: color16 = MAKE_RGBA_5551(r,g,b,0xff); *(p16+x) = color16; break; case DISPLAY_PIXEL_FORMAT_4444: color16 = MAKE_RGBA_4444(r,g,b,0xff); *(p16+x) = color16; break; case DISPLAY_PIXEL_FORMAT_8888: color32 = MAKE_RGBA_8888(r,g,b,0xff); *(p32+x) = color32; break; } p+=3; } p32 += texw; p16 += texw; } SAFE_FREE(scanline); jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); pimage = (image_p)malloc(sizeof(image_t)); memset(pimage,0,sizeof(image_t)); pimage->data = (uint8 *)data; pimage->w = width; pimage->h = height; pimage->texw = texw; pimage->texh = texh; pimage->bpb = bpb; pimage->dtype = displaymode; pimage->rcentrex = pimage->w*1.0f/2; pimage->rcentrey = pimage->h*1.0f/2; pimage->mode = GET_PSM_COLOR_MODE(displaymode); pimage->mask = CreateColor(255,255,255,255,displaymode); pimage->texid = image_tid++; return pimage; }
static void draw_one_word(PFontFreetype pf,FT_Bitmap* bitmap,image_p pimage,int x,int y) { int height = bitmap->rows; int width = bitmap->width; int i,j; uint32_t* cpbegin32; uint16_t* cpbegin16; unsigned char *buf = bitmap->buffer; if(y + height > (int)pimage->h) height = pimage->h - y; if(x + width > (int)pimage->w) width = pimage->w - x; if(height <= 0 || width <= 0) return; switch(pimage->dtype) { case DISPLAY_PIXEL_FORMAT_8888: cpbegin32 = (uint32_t*)pimage->data + y * pimage->texw + x; if(y < 0) { cpbegin32 -= y * pimage->texw; buf -= y * bitmap->width; j = -y; } else j = 0; for(; j < height; j++){ if(x < 0) { cpbegin32 -= x; buf -= x; i = -x; } else i = 0; for(; i < width; i++) *(cpbegin32++) = MAKE_RGBA_8888(pf->r, pf->g, pf->b, pf->alpha_table[*(buf++)]); cpbegin32 += pimage->texw - width; buf += bitmap->width - width; } break; case DISPLAY_PIXEL_FORMAT_4444: cpbegin16 = (uint16_t*)pimage->data + y * pimage->texw + x; if(y < 0) { cpbegin16 -= y * pimage->texw; buf -= y * bitmap->width; j = -y; } else j = 0; for(; j < height; j++){ if(x < 0) { cpbegin16 -= x; buf -= x; i = -x; } else i = 0; for(; i < width; i++) *(cpbegin16++) = MAKE_RGBA_4444(pf->r, pf->g, pf->b, pf->alpha_table[*(buf++)]); cpbegin16 += pimage->texw - width; buf += bitmap->width - width; } break; case DISPLAY_PIXEL_FORMAT_5551: cpbegin16 = (uint16_t*)pimage->data + y * pimage->texw + x; if(y < 0) { cpbegin16 -= y * pimage->texw; buf -= y * bitmap->width; j = -y; } else j = 0; for(; j < height; j++){ if(x < 0) { cpbegin16 -= x; buf -= x; i = -x; } else i = 0; for(; i < width; i++) *(cpbegin16++) = MAKE_RGBA_5551(pf->r, pf->g, pf->b, (pf->a&(*(buf++)))?255:0); cpbegin16 += pimage->texw - width; buf += bitmap->width - width; } break; case DISPLAY_PIXEL_FORMAT_565: cpbegin16 = (uint16_t*)pimage->data + y * pimage->texw + x; if(y < 0) { cpbegin16 -= y * pimage->texw; buf -= y * bitmap->width; j = -y; } else j = 0; for(; j < height; j++){ if(x < 0) { cpbegin16 -= x; buf -= x; i = -x; } else i = 0; for(; i < width; i++) if(*(buf++)) *(cpbegin16++) = MAKE_RGBA_565(pf->r, pf->g, pf->b, 0); else *(cpbegin16++) = 0; cpbegin16 += pimage->texw - width; buf += bitmap->width - width; } break; default: break; } }
#include "libnge2.h" #include "CppSQLite3.h" #include <stdlib.h> #include "nge_charsets.h" /** * nge_test:测试 sqlite3 */ int game_quit = 0; image_p pimage_text,pimage_bg,pimage_box; image_p pimage_icon[2]; int mask8888 = MAKE_RGBA_8888(255,255,255,255); int mask4444 = MAKE_RGBA_4444(255,255,255,255); //半透明的box int maskbox = MAKE_RGBA_8888(255,255,255,128); CppSQLite3DB db; char* CreateInfoByid(int id) { static char buffer[1024]={0}; static char querybuf[1024]={0}; char* str = "%s 体力%s 武力%s 智力%s 魅力%s 年龄%s 类型 %s"; memset(querybuf,0,1024); try { db.open("database/infodata.db"); sprintf(querybuf,"select heroinfo.name,ti,wu,zhi,mei,age,herotype.name from " " heroinfo,herotype where heroinfo.id=%d and heroinfo.type=herotype.id;",id);
//image conv image_p image_conv(image_p src, int dtype) { image_p dst; uint32_t i,j; uint32_t *src32, *dst32; uint16_t *src16, *dst16; uint8_t r,g,b,a; if(src->dtype == (uint32_t)dtype) return image_clone(src); CHECK_AND_UNSWIZZLE(src); dst = image_create(src->w, src->h, dtype); src32 = (uint32_t*)src->data; dst32 = (uint32_t*)dst->data; src16 = (uint16_t*)src->data; dst16 = (uint16_t*)dst->data; for(i = 0; i < src->h; i++) { for (j = 0; j<src->w; j++) { if(dtype == DISPLAY_PIXEL_FORMAT_8888){ if(src->dtype == DISPLAY_PIXEL_FORMAT_4444){ GET_RGBA_4444(src16[i*src->texw+j],r,g,b,a); dst32[i*dst->texw+j] = MAKE_RGBA_8888(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_5551){ GET_RGBA_5551(src16[i*src->texw+j],r,g,b,a); dst32[i*dst->texw+j] = MAKE_RGBA_8888(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_565){ GET_RGBA_565(src16[i*src->texw+j],r,g,b,a); dst32[i*dst->texw+j] = MAKE_RGBA_8888(r,g,b,a); } } else if(dtype == DISPLAY_PIXEL_FORMAT_4444){ if(src->dtype == DISPLAY_PIXEL_FORMAT_8888){ GET_RGBA_8888(src32[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_4444(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_5551){ GET_RGBA_5551(src16[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_4444(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_565){ GET_RGBA_565(src16[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_4444(r,g,b,a); } } else if(dtype == DISPLAY_PIXEL_FORMAT_5551){ if(src->dtype == DISPLAY_PIXEL_FORMAT_8888){ GET_RGBA_8888(src32[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_5551(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_4444){ GET_RGBA_4444(src16[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_5551(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_565){ GET_RGBA_565(src16[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_5551(r,g,b,a); } } else if(dtype == DISPLAY_PIXEL_FORMAT_565){ if(src->dtype == DISPLAY_PIXEL_FORMAT_8888){ GET_RGBA_8888(src32[i*src->texh+j],r,g,b,a); dst16[i*dst->texh+j] = MAKE_RGBA_565(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_4444){ GET_RGBA_4444(src16[i*src->texh+j],r,g,b,a); dst16[i*dst->texh+j] = MAKE_RGBA_565(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_5551){ GET_RGBA_5551(src16[i*src->texh+j],r,g,b,a); dst16[i*dst->texh+j] = MAKE_RGBA_565(r,g,b,a); } } } } dst->swizzle = 1; swizzle_swap(dst); CHECK_AND_SWIZZLE(src); return dst; }
int get_saturation_brightness_color(int dtype, int scol,int saturation, int brightness) { int r, g, b, a, mincol, maxcol; switch(dtype) { case DISPLAY_PIXEL_FORMAT_8888: GET_RGBA_8888(scol, r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_4444: GET_RGBA_4444(scol, r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_5551: GET_RGBA_5551(scol, r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_565: GET_RGBA_565(scol, r, g, b, a); break; default: return 0; } // 处理饱和度 if(saturation!=0) { maxcol = r>g ? (r>b?r:b):g; mincol = r>g ? (g>b?b:g):r; if(saturation>0) { if(maxcol == 0xff) { goto cont001; } } else { if(maxcol == 0) { goto cont001; } } r += mincol + (r - mincol)*(100 + saturation)/100; g = mincol + (g - mincol)*(100 + saturation)/100; b = mincol + (b - mincol)*(100 + saturation)/100; if(r>0xff)r=0xff; if(g>0xff)g=0xff; if(b>0xff)b=0xff; if(r<0)r=0; if(g<0)g=0; if(b<0)b=0; } cont001: // 处理亮度 if(brightness!=0) { //gcol = ((r + g + b) / 3) & 0xff; //maxcol = r>g ? (r>b?r:b):g; mincol = r>g ? (g>b?b:g):r; r += (0xff-mincol) * (brightness)/100; g += (0xff-mincol) * (brightness)/100; b += (0xff-mincol) * (brightness)/100; if(r>0xff)r=0xff; if(g>0xff)g=0xff; if(b>0xff)b=0xff; if(r<0)r=0; if(g<0)g=0; if(b<0)b=0; } // 输出 switch(dtype) { case DISPLAY_PIXEL_FORMAT_8888: return MAKE_RGBA_8888(r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_4444: return MAKE_RGBA_4444(r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_5551: return MAKE_RGBA_5551(r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_565: return MAKE_RGBA_565(r, g, b, a); break; } return 0; }
int get_gray_color(int dtype, int scol, int gray) { int gcol = 0, r = 0, g = 0, b = 0, a = 0; if(gray==0) return scol; switch(dtype) { case DISPLAY_PIXEL_FORMAT_8888: GET_RGBA_8888(scol, r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_4444: GET_RGBA_4444(scol, r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_5551: GET_RGBA_5551(scol, r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_565: GET_RGBA_565(scol, r, g, b, a); break; } gcol = ((r + g + b) / 3) & 0xff; if(gray==100) { switch(dtype) { case DISPLAY_PIXEL_FORMAT_8888: return MAKE_RGBA_8888(gcol, gcol, gcol, a); break; case DISPLAY_PIXEL_FORMAT_4444: return MAKE_RGBA_4444(gcol, gcol, gcol, a); break; case DISPLAY_PIXEL_FORMAT_5551: return MAKE_RGBA_5551(gcol, gcol, gcol, a); break; case DISPLAY_PIXEL_FORMAT_565: return MAKE_RGBA_565(gcol, gcol, gcol, a); break; } } r += ((gcol - r) * gray / 100) & 0xff; g += ((gcol - g) * gray / 100) & 0xff; b += ((gcol - b) * gray / 100) & 0xff; switch(dtype) { case DISPLAY_PIXEL_FORMAT_8888: return MAKE_RGBA_8888(r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_4444: return MAKE_RGBA_4444(r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_5551: return MAKE_RGBA_5551(r, g, b, a); break; case DISPLAY_PIXEL_FORMAT_565: return MAKE_RGBA_565(r, g, b, a); break; } return 0; }
image_p create_contrast_image(image_p src, int contrast){ uint32 y,x; uint8 recover = 0; image_p pimg; uint16 *p16,*psrc16; uint32 *p32,*psrc32; sint16 r,g,b,a; sint32 col; uint8 colmap[255]; if(contrast<-100 || contrast>100) { nge_print("create_contrast_image arg 'contrast' must between -100 to 100!"); return NULL; } contrast+=100; if(contrast == 100) return image_clone(src); if(src->swizzle ==1){ unswizzle_swap(src); recover = 1; } for(x=0;x<256;x++){ if(contrast < 100){ colmap[x] = max(0, min((int)(x - 128) * (contrast / 100) + 128, 255)); } else{ colmap[x] = max(0, min((int)(x - 128) * ((contrast - 100) * 1.28) + 128, 255)); } } pimg = image_create(src->w, src->h, src->dtype); for(y=0;y<src->h;y++) { for(x=0;x<src->w;x++){ if(src->dtype == DISPLAY_PIXEL_FORMAT_8888) { p32 = (uint32 *)pimg->data + y*src->texw; psrc32 = (uint32 *)src->data + y*src->texw; } else { p16 = (uint16 *)pimg->data + y*src->texw; psrc16 = (uint16 *)src->data + y*src->texw; } if(src->dtype == DISPLAY_PIXEL_FORMAT_4444){ col = *(psrc16 + x); GET_RGBA_4444(col, r,g,b,a); if(a > 0) { r = colmap[r]; g = colmap[g]; b = colmap[b]; } *(p16 + x) = MAKE_RGBA_4444(r,g,b,a); } } } if(recover) swizzle_swap(src); return pimg; }
image_p image_conv(image_p src, int dtype) { image_p dst; uint8 recover = 0; uint32 i,j; uint32 *src32, *dst32; uint16 *src16, *dst16; uint8 r,g,b,a; if(src->dtype == dtype) return image_clone(src); if(src->swizzle ==1){ unswizzle_swap(src); recover = 1; } dst = image_create(src->w, src->h, dtype); src32 = (uint32*)src->data; dst32 = (uint32*)dst->data; src16 = (uint16*)src->data; dst16 = (uint16*)dst->data; for(i = 0; i < src->h; i++) { for (j = 0; j<src->w; j++) { if(dtype == DISPLAY_PIXEL_FORMAT_8888){ if(src->dtype == DISPLAY_PIXEL_FORMAT_4444){ GET_RGBA_4444(src16[i*src->texw+j],r,g,b,a); dst32[i*dst->texw+j] = MAKE_RGBA_8888(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_5551){ GET_RGBA_5551(src16[i*src->texw+j],r,g,b,a); dst32[i*dst->texw+j] = MAKE_RGBA_8888(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_565){ GET_RGBA_565(src16[i*src->texw+j],r,g,b,a); dst32[i*dst->texw+j] = MAKE_RGBA_8888(r,g,b,a); } } else if(dtype == DISPLAY_PIXEL_FORMAT_4444){ if(src->dtype == DISPLAY_PIXEL_FORMAT_8888){ GET_RGBA_8888(src32[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_4444(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_5551){ GET_RGBA_5551(src16[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_4444(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_565){ GET_RGBA_565(src16[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_4444(r,g,b,a); } } else if(dtype == DISPLAY_PIXEL_FORMAT_5551){ if(src->dtype == DISPLAY_PIXEL_FORMAT_8888){ GET_RGBA_8888(src32[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_5551(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_4444){ GET_RGBA_4444(src16[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_5551(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_565){ GET_RGBA_565(src16[i*src->texw+j],r,g,b,a); dst16[i*dst->texw+j] = MAKE_RGBA_5551(r,g,b,a); } } else if(dtype == DISPLAY_PIXEL_FORMAT_565){ if(src->dtype == DISPLAY_PIXEL_FORMAT_8888){ GET_RGBA_8888(src32[i*src->texh+j],r,g,b,a); dst16[i*dst->texh+j] = MAKE_RGBA_565(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_4444){ GET_RGBA_4444(src16[i*src->texh+j],r,g,b,a); dst16[i*dst->texh+j] = MAKE_RGBA_565(r,g,b,a); } else if(src->dtype == DISPLAY_PIXEL_FORMAT_5551){ GET_RGBA_5551(src16[i*src->texh+j],r,g,b,a); dst16[i*dst->texh+j] = MAKE_RGBA_565(r,g,b,a); } } } } swizzle_swap(dst); if(recover) swizzle_swap(src); return dst; }
image_p image_rot(image_p src,float angel) { image_p dst; uint8 recover = 0; double sx,rs0,rs1,sy; double rangel; pointf d0,d1,d2,d3,d4; float x1,x2,y1,y2; sint16 xo, yo, wo, ho; uint32 i,j; uint32 *src32, *dst32; uint16 *src16, *dst16; uint16 r,g,b,a; uint16 r2,g2,b2,a2; uint8 smc; float xbx; while(angel > 360.0f) angel-=360.0f; while(angel<0) angel+=360.0f; if(angel == 0) return image_clone(src); if(src->swizzle ==1){ unswizzle_swap(src); recover = 1; } rangel = angel*3.1415926f/180.0f; sx = cos(rangel); rs0 = sin(rangel); rs1 = -sin(rangel); sy = cos(rangel); d0.x = 0; d0.y = 0; d1 = image_rot_pointf(d0, sx, sy, rs0, rs1); d0.x = src->w; d2 = image_rot_pointf(d0, sx, sy, rs0, rs1); d0.y = src->h; d3 = image_rot_pointf(d0, sx, sy, rs0, rs1); d0.x = 0; d4 = image_rot_pointf(d0, sx, sy, rs0, rs1); x1 = min(d1.x, min(d2.x, min(d3.x, d4.x))); x2 = max(d1.x, max(d2.x, max(d3.x, d4.x))); y1 = min(d1.y, min(d2.y, min(d3.y, d4.y))); y2 = max(d1.y, max(d2.y, max(d3.y, d4.y))); xo = x1; yo = y1; wo = x2 - xo; ho = y2 - yo; dst = image_create(wo, ho, src->dtype); src32 = (uint32*)src->data; dst32 = (uint32*)dst->data; src16 = (uint16*)src->data; dst16 = (uint16*)dst->data; for(i = 0; i < dst->h; i++) { for (j = 0; j<dst->w; j++) { d0.x = j; d0.y = i; d0 = image_rot_pointf(d0, sx, sy, rs0, rs1); // easy way smc = 1; r2 = 0; g2 = 0; b2 = 0; a2 = 0; if(src->dtype == DISPLAY_PIXEL_FORMAT_8888){ if((int)d0.y-yo < src->texh && (int)d0.x-xo < src->texw){ //dst16[i*dst->texw+j] = src16[((int)d0.y-yo)*src->texw+(int)d0.x-xo]; GET_RGBA_8888(src32[((int)d0.y-yo)*src->texw+(int)d0.x-xo], r, g, b, a); xbx = 1.0f - ((d0.y-yo) - (int)(d0.y-yo)); r2 = r * xbx; g2 = g * xbx; b2 = b * xbx; a2 = a * xbx; if((int)d0.y-yo+1 < src->texh){ smc++; xbx = 1.0f - xbx; GET_RGBA_8888(src32[((int)d0.y-yo+1)*src->texw+(int)d0.x-xo], r, g, b, a); r2 += r * xbx; g2 += g * xbx; b2 += b * xbx; a2 += a * xbx; } r = min(r2, 255); g = min(g2, 255); b = min(b2, 255); a = min(a2, 255); dst32[i*dst->texw+j] = MAKE_RGBA_8888(r,g,b,a); } } else if(src->dtype == DISPLAY_PIXEL_FORMAT_4444){ if((int)d0.y-yo < src->texh && (int)d0.x-xo < src->texw){ //dst16[i*dst->texw+j] = src16[((int)d0.y-yo)*src->texw+(int)d0.x-xo]; GET_RGBA_4444(src16[((int)d0.y-yo)*src->texw+(int)d0.x-xo], r, g, b, a); xbx = 1.0f - ((d0.y-yo) - (int)(d0.y-yo)); r2 = r * xbx; g2 = g * xbx; b2 = b * xbx; a2 = a * xbx; if((int)d0.y-yo+1 < src->texh){ smc++; xbx = 1.0f - xbx; GET_RGBA_4444(src16[((int)d0.y-yo+1)*src->texw+(int)d0.x-xo], r, g, b, a); r2 += r * xbx; g2 += g * xbx; b2 += b * xbx; a2 += a * xbx; } r = min(r2, 255); g = min(g2, 255); b = min(b2, 255); a = min(a2, 255); dst16[i*dst->texw+j] = MAKE_RGBA_4444(r,g,b,a); } } else if(src->dtype == DISPLAY_PIXEL_FORMAT_5551){ if((int)d0.y-yo < src->texh && (int)d0.x-xo < src->texw){ //dst16[i*dst->texw+j] = src16[((int)d0.y-yo)*src->texw+(int)d0.x-xo]; GET_RGBA_5551(src16[((int)d0.y-yo)*src->texw+(int)d0.x-xo], r, g, b, a); xbx = 1.0f - ((d0.y-yo) - (int)(d0.y-yo)); r2 = r * xbx; g2 = g * xbx; b2 = b * xbx; a2 = a * xbx; if((int)d0.y-yo+1 < src->texh){ smc++; xbx = 1.0f - xbx; GET_RGBA_5551(src16[((int)d0.y-yo+1)*src->texw+(int)d0.x-xo], r, g, b, a); r2 += r * xbx; g2 += g * xbx; b2 += b * xbx; a2 += a * xbx; } r = min(r2, 255); g = min(g2, 255); b = min(b2, 255); a = min(a2, 255); dst16[i*dst->texw+j] = MAKE_RGBA_5551(r,g,b,a); } } else{ if((int)d0.y-yo < src->texh && (int)d0.x-xo < src->texw){ //dst16[i*dst->texw+j] = src16[((int)d0.y-yo)*src->texw+(int)d0.x-xo]; GET_RGBA_565(src16[((int)d0.y-yo)*src->texw+(int)d0.x-xo], r, g, b, a); xbx = 1.0f - ((d0.y-yo) - (int)(d0.y-yo)); r2 = r * xbx; g2 = g * xbx; b2 = b * xbx; a2 = a * xbx; if((int)d0.y-yo+1 < src->texh){ smc++; xbx = 1.0f - xbx; GET_RGBA_565(src16[((int)d0.y-yo+1)*src->texw+(int)d0.x-xo], r, g, b, a); r2 += r * xbx; g2 += g * xbx; b2 += b * xbx; a2 += a * xbx; } r = min(r2, 255); g = min(g2, 255); b = min(b2, 255); a = min(a2, 255); dst16[i*dst->texw+j] = MAKE_RGBA_565(r,g,b,a); } } } } swizzle_swap(dst); if(recover) swizzle_swap(src); return dst; }
image_p image_load_bmp_buf(const char* mbuf,int bsize, int displaymode) { image_p pimage = NULL; uint8_t *pdata,*data,*line; int dsize,w,h,texw,texh,bpb,size,x,y,done = 0; uint32_t* p32; uint16_t* p16; uint16_t color16; uint32_t color32; PBITMAPFILEHEADER pbfh = (PBITMAPFILEHEADER)mbuf; PBITMAPINFOHEADER pbih; if(pbfh->bfType !=0x4d42){ nge_print("not bmp file\n"); return 0; } pbih = (PBITMAPINFOHEADER)(mbuf+sizeof(BITMAPFILEHEADER)); dsize = sizeof(BITMAPFILEHEADER)+pbih->biSize; pdata = (uint8_t*)mbuf+dsize; w = pbih->biWidth; h = pbih->biHeight; texw = roundpower2(w); texh = roundpower2(h); bpb = 4; if(displaymode != DISPLAY_PIXEL_FORMAT_8888){ bpb = 2; } //int biSizeImage = ((((pbih->biWidth * pbih->biBitCount) + 31) & ~31) / 8) * pbih->biHeight; size = texw * texh * bpb; data = (uint8_t*)malloc(size); memset(data,0,size); p32 = (uint32_t*)data; p16 = (uint16_t*) p32; line = NULL; if(pbih->biBitCount == 24){ for (y = h;y>0;y--){ line = pdata+(y-1)*w*3; for (x=0;x<w;x++){ switch(displaymode) { case DISPLAY_PIXEL_FORMAT_8888: color32 = MAKE_RGBA_8888(line[2],line[1],line[0],0xff); *(p32+x) = color32; break; case DISPLAY_PIXEL_FORMAT_565: color16 = MAKE_RGBA_565(line[2],line[1],line[0],0xff); *(p16+x) = color16; break; case DISPLAY_PIXEL_FORMAT_5551: color16 = MAKE_RGBA_5551(line[2],line[1],line[0],0xff); *(p16+x) = color16; break; case DISPLAY_PIXEL_FORMAT_4444: color16 = MAKE_RGBA_4444(line[2],line[1],line[0],0xff); *(p16+x) = color16; break; } line+=3; } p32 += texw; p16 += texw; } done = 1; } else if(pbih->biBitCount == 32){ for (y = h;y>0;y--){ line = pdata+(y-1)*w*4; for (x=0;x<w;x++){ switch(displaymode) { case DISPLAY_PIXEL_FORMAT_8888: color32 = MAKE_RGBA_8888(line[2],line[1],line[0],0xff); *(p32+x) = color32; break; case DISPLAY_PIXEL_FORMAT_565: color16 = MAKE_RGBA_565(line[2],line[1],line[0],0xff); *(p16+x) = color16; break; case DISPLAY_PIXEL_FORMAT_5551: color16 = MAKE_RGBA_5551(line[2],line[1],line[0],0xff); *(p16+x) = color16; break; case DISPLAY_PIXEL_FORMAT_4444: color16 = MAKE_RGBA_4444(line[2],line[1],line[0],0xff); *(p16+x) = color16; break; } line+=4; } p32 += texw; p16 += texw; } done = 1; } if (done){ pimage = (image_p)malloc(sizeof(image_t)); memset(pimage,0,sizeof(image_t)); pimage->data = (uint8_t *)data; pimage->w = w; pimage->h = h; pimage->texw = texw; pimage->texh = texh; pimage->bpb = bpb; pimage->dtype = displaymode; pimage->rcentrex = pimage->w*1.0f/2; pimage->rcentrey = pimage->h*1.0f/2; pimage->mode = GET_PSM_COLOR_MODE(displaymode); pimage->mask = CreateColor(255,255,255,255,displaymode); pimage->texid = image_tid++; } return pimage; }
int main(int argc, char* argv[]) { NGE_Init(INIT_ALL); InitInput(btn_down,btn_up,1); PFont pf[2] ; int i; //创建一个显示image,字就显示在这个上面注意DISPLAY_PIXEL_FORMAT必须与创建字体的DISPLAY_PIXEL_FORMAT一致 pimage_text = image_create(512,512,DISPLAY_PIXEL_FORMAT_4444); //创建字体 pf[0] = create_font_hzk("fonts/GBK14","fonts/ASC14",14,DISPLAY_PIXEL_FORMAT_4444); pf[1] = create_font_freetype("fonts/simfang.ttf",13,DISPLAY_PIXEL_FORMAT_4444); char str1[3][128]={"【报幕员】","下面播放的是由雪莉演唱的射手座。","欢迎大家收听!"}; char str2[3][128]={"【雪莉粉丝】","哇哇哇。。。太棒了!","XXXX女王SAMA,我爱你。"}; //显示GBK Font font_setcolor(pf[0],MAKE_RGBA_4444(128,0,0,255)); font_drawtext(pf[0],str2[0],strlen(str2[0]),pimage_text,100,195,FONT_SHOW_NORMAL); for(i = 1;i<3;i++){ font_drawtext(pf[0],str2[i],strlen(str2[i]),pimage_text,120,200+i*20,FONT_SHOW_SHADOW); font_setcolor(pf[0],MAKE_RGBA_4444(255,0,0,255)); } //显示freetype font_setcolor(pf[1],MAKE_RGBA_4444(128,0,0,255)); font_drawtext(pf[1],str1[0],strlen(str1[0]),pimage_text,100,30,FONT_SHOW_NORMAL); for(i =1;i<3;i++){ font_drawtext(pf[1],str1[i],strlen(str1[i]),pimage_text,120,35+i*20,FONT_SHOW_NORMAL); font_setcolor(pf[1],MAKE_RGBA_4444(255,0,0,255)); } pimage_bg = image_load("images/demo2_bg.jpg",DISPLAY_PIXEL_FORMAT_8888,1); if(pimage_bg == NULL) printf("can not open file\n"); pimage_box = image_load("images/demo2_box.jpg",DISPLAY_PIXEL_FORMAT_8888,1); if(pimage_box == NULL) printf("can not open file\n"); pimage_icon[0] = image_load_colorkey("images/demo2_icon1.png",DISPLAY_PIXEL_FORMAT_8888,MAKE_RGB(0,0,0),1); pimage_icon[1] = image_load_colorkey("images/demo2_icon0.bmp",DISPLAY_PIXEL_FORMAT_8888,MAKE_RGB(0,0,0),1); //初始化Mp3播放结构 MP3PlayInit(&mp3_play); PCMPlayInit(&wav_play); //设置音量 mp3_play.volume(volume); //加载 music/demo2.mp3 int res = mp3_play.load("music/simple1.mp3"); //开始播放用play需要调用stop释放资源 mp3_play.play(); while ( !game_quit ) { ShowFps(); InputProc(); DrawScene(); } font_destory(pf[0]); font_destory(pf[1]); image_free(pimage_bg); image_free(pimage_text); image_free(pimage_box); //退出mp3系统 MP3PlayFini(); PCMPlayFini(); NGE_Quit(); return 0; }