1 Star 0 Fork 0

会写代码的猴子 / SDLTest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
graphics.c 10.21 KB
一键复制 编辑 原始数据 按行查看 历史
fengdeyingzi 提交于 2020-08-16 20:17 . 实现clipBitmap
/*
SDL 实现手机C的graphics.h
*/
#include "graphics.h"
#include "base.h"
static SDL_Renderer *renderer;
static SDL_Window *window;
static TTF_Font *fonts[3];
int createBitmap(int w, int h){
int rmask = 0xFF000000; int gmask = 0x00FF0000; int bmask = 0x0000FF00; int amask = 0x000000FF; // RGBA8888模式
SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h , 32, rmask, gmask, bmask, amask);
return (int)surface;
}
static int min(int num1,int num2){
return num1<num2? num1 : num2;
}
void graphics_init(SDL_Window *win,SDL_Renderer *render)
{
renderer = render;
window = win;
//初始化SDL_ttf
if (TTF_Init() == -1)
{
printf("ttf_init error\n");
}
fonts[0] = TTF_OpenFont("assets/font.ttf", 16);
fonts[1] = TTF_OpenFont("assets/font.ttf", 20);
fonts[2] = TTF_OpenFont("assets/font.ttf", 24);
if (!fonts[0])
{
printf("TTF_OpenFont: %s\n", TTF_GetError());
}
if (!fonts[1])
{
printf("TTF_OpenFont: %s\n", TTF_GetError());
}
if (!fonts[2])
{
printf("TTF_OpenFont: %s\n", TTF_GetError());
}
//设置支持 Alpha
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
}
void setDrawRenderer(SDL_Renderer *render)
{
renderer = render;
}
void drawText(char *text, int x, int y,unsigned int color, int is_unicode, int font_type)
{
//打开字库
// TTF_Font *font;
// font=TTF_OpenFont("font.ttf", 64);
// if(!font) {
// printf("TTF_OpenFont: %s\n", TTF_GetError());
// }
SDL_Color scolor = {(color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff, (color >> 24) & 0xff}; //颜色
SDL_Surface *text_surface;
SDL_Texture *pTexture = NULL;
if(is_unicode){
text_surface = TTF_RenderUNICODE_Solid(fonts[font_type], (Uint16*)text, scolor);
}
else
text_surface = TTF_RenderUTF8_Solid(fonts[font_type], text, scolor);
if (text_surface != NULL)
{
pTexture = SDL_CreateTextureFromSurface(renderer, text_surface);
// SDL_FreeSurface(text_surface);
SDL_Rect srcrect = {0, 0, text_surface->w, text_surface->h};
SDL_Rect dstrect = {x, y, text_surface->w, text_surface->h};
//往渲染器上copy字体
//<-----------------第二步:渲染文字即可----------------------------->
SDL_RenderCopy(renderer, pTexture, &srcrect, &dstrect);
SDL_DestroyTexture(pTexture);
printf("drawText 1\n");
// drawBitmap(x,y, (int)text_surface);
// SDL_BlitSurface(text_surface, NULL, renderer, NULL);//将文字复制到屏幕的surface上面
SDL_FreeSurface(text_surface);
printf("drawText ok\n");
return;
}
else
{
// report error
printf("drawText error\n");
return;
}
}
void drawTextEx(char *text, int x,int y, rectst *rect, unsigned int color, int is_unicode, int font_type)
{
//打开字库
// TTF_Font *font;
// font=TTF_OpenFont("font.ttf", 64);
// if(!font) {
// printf("TTF_OpenFont: %s\n", TTF_GetError());
// }
SDL_Color scolor = {(color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff, (color >> 24) & 0xff}; //颜色
SDL_Surface *text_surface;
SDL_Texture *pTexture = NULL;
if(is_unicode){
text_surface = TTF_RenderUNICODE_Blended_Wrapped(fonts[font_type], (Uint16*)text, scolor, rect->w);
}
else
text_surface = TTF_RenderUTF8_Blended_Wrapped(fonts[font_type], text, scolor, rect->w);
SDL_Rect cliprect = {rect->x, rect->y, rect->w, rect->h};
int window_w,window_h;
SDL_GetWindowSize(window, &window_w, &window_h);
SDL_Rect allrect = {0, 0, window_w, window_h};
if (text_surface != NULL)
{
pTexture = SDL_CreateTextureFromSurface(renderer, text_surface);
// SDL_FreeSurface(text_surface);
int dw = min(rect->w, text_surface->w);
int dh = min(rect->h, text_surface->h);
SDL_Rect srcrect = {0, 0, text_surface->w, text_surface->h};
SDL_Rect dstrect = {x, y, text_surface->w, text_surface->h};
SDL_RenderSetClipRect(renderer, &cliprect);
//往渲染器上copy字体
//<-----------------第二步:渲染文字即可----------------------------->
SDL_RenderCopy(renderer, pTexture, &srcrect, &dstrect);
SDL_DestroyTexture(pTexture);
printf("drawText 1\n");
// drawBitmap(x,y, (int)text_surface);
// SDL_BlitSurface(text_surface, NULL, renderer, NULL);//将文字复制到屏幕的surface上面
SDL_FreeSurface(text_surface);
SDL_RenderSetClipRect(renderer, &allrect);
printf("drawText ok\n");
return;
}
else
{
// report error
printf("drawText error\n");
return;
}
}
void getTextWH(char *text,int is_unicode, int font_type, int *w, int *h){
if(is_unicode){
TTF_SizeUNICODE(fonts[font_type], (Uint16*)text, w, h);
}
else
TTF_SizeUTF8(fonts[font_type], text, w,h);
}
int readBitmap(char *filename)
{
SDL_Surface *surface = NULL;
SDL_Texture *pTexture = NULL;
// surface = SDL_LoadBMP(filename);
if(strncmp(filename,"assets://",9)==0){
return readBitmapFromAssets(filename+9);
}
surface = IMG_Load(filename);
// //使用SDL_Surface创建Texture
// pTexture = SDL_CreateTextureFromSurface(renderer, surface);
return (int)surface;
}
int clipBitmap(int bmp, int x,int y,int w,int h){
SDL_Surface *surface = (SDL_Surface *)bmp;
SDL_Surface *newsurface = (SDL_Surface *)createBitmap(w,h);
SDL_Rect srcrect = {x,y,w,h};
SDL_Rect dstrect = {0,0,w,h};
SDL_UpperBlit(surface, &srcrect, newsurface, &dstrect);
return (int)newsurface;
}
int drawBitmapOld(int di, int buf, int x,int y,int w,int h, int sw, int sh){
return 0;
}
int saveBitmap(int bmp, char *filename, int type, int load){
return 0;
}
int bitmapGetInfo(int bmp, _BITMAPINFO *info){
SDL_Surface *surface = (SDL_Surface *)bmp;
info->width = surface->w;
info->height = surface->h;
info->format = surface->format->format;
info->ptr = surface->pixels;
return 0;
}
int readBitmapFromAssets(char *filename)
{
SDL_Surface *surface = NULL;
SDL_Texture *pTexture = NULL;
char text[300];
sprintf(text, "assets\\%s", filename);
surface = IMG_Load(text);
// //使用SDL_Surface创建Texture
// pTexture = SDL_CreateTextureFromSurface(renderer, surface);
return (int)surface;
}
void drawBitmap(int bmp, int x, int y)
{
SDL_Surface *surface = (SDL_Surface *)bmp;
SDL_Texture *pTexture = NULL;
SDL_Rect srcRect = {0, 0, surface->w, surface->h};
SDL_Rect dstRect = {x, y, surface->w, surface->h};
pTexture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderCopy(renderer, pTexture, &srcRect, &dstRect);
SDL_DestroyTexture(pTexture);
}
void drawBitmapFlip(int bmp, int x, int y, int w, int h, int tx, int ty)
{
SDL_Surface *surface = (SDL_Surface *)bmp;
SDL_Texture *pTexture = NULL;
SDL_Rect srcRect = {tx, ty, w, h};
SDL_Rect dstRect = {x, y, w, h};
pTexture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderCopy(renderer, pTexture, &srcRect, &dstRect);
SDL_DestroyTexture(pTexture);
}
void drawBitmapEx(int bmp, int x, int y, int w, int h, int tx, int ty, int tw, int th)
{
SDL_Surface *surface = (SDL_Surface *)bmp;
SDL_Texture *pTexture = NULL;
SDL_Rect srcRect = {tx, ty, tw, th};
SDL_Rect dstRect = {x, y, w, h};
pTexture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderCopy(renderer, pTexture, &srcRect, &dstRect);
SDL_DestroyTexture(pTexture);
}
void drawBitmapRotate(int bmp, int scrx, int scry, int bx,int by, int r, int alpha){
SDL_Surface *surface = (SDL_Surface *)bmp;
SDL_Texture *pTexture = NULL;
SDL_Rect srcRect = {0, 0, surface->w, surface->h};
SDL_Rect dstRect = {scrx, scry, surface->w, surface->h};
pTexture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Point point = {bx,by};
SDL_RenderCopyEx(renderer, pTexture, &srcRect, &dstRect, r, &point, SDL_FLIP_NONE);
SDL_DestroyTexture(pTexture);
}
//释放已加载的图像
void bitmapFree(int bmp)
{
SDL_Surface *surface = (SDL_Surface *)bmp;
SDL_FreeSurface(surface);
}
//
void drawPoint(int x,int y,unsigned int color){
SDL_SetRenderDrawColor(renderer, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff, (color >> 24) & 0xff);
SDL_RenderDrawPoint(renderer,x,y);
}
//画矩形
void drawRect(int x, int y, int w, int h, unsigned int color)
{
SDL_SetRenderDrawColor(renderer, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff, (color >> 24) & 0xff);
SDL_Rect rect = {x, y, w, h};
SDL_RenderFillRect(renderer, &rect); //
}
void drawCircle(int x, int y, int radius, unsigned int color)
{
// using std::cout;
// using std::clock;
// using std::endl;
// int st=clock();
int i = 0;
int tx = 0, ty = radius, d = 3 - (radius << 1);
SDL_SetRenderDrawColor(renderer, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff, (color >> 24) & 0xff);
while (tx < ty)
{
for (i = x - ty; i <= x + ty; ++i)
{
SDL_RenderDrawPoint(renderer, i, y - tx);
if (tx)
SDL_RenderDrawPoint(renderer, i, y + tx);
}
if (d < 0)
d += (tx << 2) + 6;
else
{
for (i = x - tx; i <= x + tx; ++i)
{
SDL_RenderDrawPoint(renderer, i, y - ty);
SDL_RenderDrawPoint(renderer, i, y + ty);
}
d += ((tx - ty) << 2) + 10, ty--;
}
tx++;
}
if (tx == ty)
for (i = x - ty; i <= x + ty; ++i)
{
SDL_RenderDrawPoint(renderer, i, y - tx);
SDL_RenderDrawPoint(renderer, i, y + tx);
}
// int en=clock();
// cout<<"Drawing Circle(radius "<<radius<<" pixels),costs:"<<en-st<<"ms"<<endl;
}
void drawLine(int x1, int y1, int x2, int y2, unsigned int color)
{
SDL_SetRenderDrawColor(renderer, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff, (color >> 24) & 0xff);
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
}
void graphics_free(){
int i=0;
//关闭用过的字体
for(i=0;i<3;i++)
TTF_CloseFont( fonts[i] );
//退出SDL_ttf
TTF_Quit();
}
1
https://gitee.com/panyunmeng/SDLTest.git
git@gitee.com:panyunmeng/SDLTest.git
panyunmeng
SDLTest
SDLTest
master

搜索帮助