zoukankan      html  css  js  c++  java
  • SDL2.0的加载图片贴图

    加载图片贴图,采用了SDL_Window、SDL_Renderer、SDL_Texture和SDL_Image库 

    实例:

      1 #include <stdio.h>
      2 #include <math.h>
      3 #include <string.h>
      4 #include <SDL2SDL.h>
      5 #include <SDL2SDL_image.h>
      6 #include <SDL2exSDL_rectex.h>
      7 
      8 SDL_Window *sdlWindow = NULL;
      9 SDL_Renderer *sdlRender = NULL;
     10 SDL_Texture *sdlTexture = NULL;
     11 SDL_Rect srcRect;
     12 SDL_Rect dstRect;
     13 int w = 500;
     14 int h = 500;
     15 
     16 bool InitView(int width, int height, const char *iconName)
     17 {
     18     //初始化窗体
     19     SDL_Init(SDL_INIT_VIDEO);
     20 
     21     sdlWindow = SDL_CreateWindow(
     22         "The First SDL Program", 
     23         SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 
     24         SDL_WINDOW_RESIZABLE);
     25     if (sdlWindow == NULL) return false;
     26 
     27     //加载窗体图标
     28     SDL_Surface *iconSurface = IMG_Load(iconName);
     29     if (iconSurface == NULL) return false;
     30 
     31     SDL_SetWindowIcon(sdlWindow, iconSurface);
     32 
     33     return true;
     34 }
     35 
     36 bool InitDraw(const char *imgName)
     37 {
     38     //加载渲染器
     39     sdlRender = SDL_CreateRenderer(sdlWindow, -1, 0);
     40     if (sdlRender == NULL) return false;
     41     SDL_SetRenderDrawColor(sdlRender, 255, 255, 255, 255);
     42 
     43     //加载绘画图片
     44     SDL_Surface *sdlSurface = IMG_Load(imgName);
     45     if (sdlSurface == NULL) return false;
     46 
     47     //加载绘画纹理
     48     sdlTexture = SDL_CreateTextureFromSurface(sdlRender, sdlSurface);
     49     if (sdlTexture == NULL) return false;
     50     
     51     SDL_FreeSurface(sdlSurface);
     52     return true;
     53 }
     54 
     55 void UpdateDraw()
     56 {
     57     SDL_RenderClear(sdlRender);
     58     
     59     //分X宫格
     60     const int count = 9;
     61     const int sqrtCount = (int)sqrt((double)count);
     62     for (int i = 0; i < sqrtCount; i++)    {
     63         srcRect = SDL_RectMake(0, 0, (w-sqrtCount)/sqrtCount, (h-sqrtCount)/sqrtCount);
     64         for (int j = 0; j < sqrtCount; j++) {
     65             srcRect.x = srcRect.w*j+(j?1*j:0);
     66             srcRect.y = srcRect.h*i+(i?1*i:0);
     67         
     68             //SDL_RectPrint("srcRect", srcRect);
     69             SDL_RectCopy(&srcRect, &dstRect);
     70             //SDL_RectPrint("dstRect", dstRect);
     71             SDL_RenderCopy(sdlRender, sdlTexture, &srcRect, &dstRect);
     72         }
     73     }
     74     
     75     SDL_RenderPresent(sdlRender);
     76 }
     77 
     78 void Quit(int code)
     79 {
     80     const char *errMsg = SDL_GetError();
     81     if (errMsg && strlen(errMsg)) {
     82         SDL_Log("Error : %s
    ", errMsg);
     83     }
     84 
     85     //销毁窗口、渲染器、纹理
     86     if (sdlWindow) SDL_DestroyWindow(sdlWindow);
     87     if (sdlRender) SDL_DestroyRenderer(sdlRender);
     88     if (sdlTexture) SDL_DestroyTexture(sdlTexture);
     89     SDL_Quit();
     90     exit(code);
     91 }
     92 
     93 void HandleKeyEvent(const SDL_Keysym* keysym)
     94 {
     95     int key = keysym->sym;
     96     switch(key)
     97     {
     98     case SDLK_ESCAPE:
     99         Quit(0);
    100         break;
    101     case SDLK_SPACE:
    102         break;
    103     case SDLK_UP:
    104     case SDLK_DOWN:
    105     case SDLK_LEFT:
    106     case SDLK_RIGHT:
    107         int x, y;
    108         SDL_GetWindowPosition(sdlWindow, &x, &y);
    109         x = (key == SDLK_LEFT ? x-2 : (key == SDLK_RIGHT ? x+2 : x));
    110         y = (key == SDLK_UP ? y-2 : (key == SDLK_DOWN ? y+2 : y));
    111         SDL_SetWindowPosition(sdlWindow, x, y);
    112         SDL_Log("x=%d, y=%d
    ", x, y);
    113         break;
    114     case SDLK_KP_PLUS:
    115     case SDLK_KP_MINUS:
    116         w = (key == SDLK_KP_PLUS ? w+2 : w-2);
    117         h = (key == SDLK_KP_PLUS ? h+2 : h-2);
    118         SDL_SetWindowSize(sdlWindow, w, h);
    119         SDL_Log("w=%d, h=%d
    ", w, h);
    120         break;
    121     default:
    122         break;
    123     }
    124 }
    125 
    126 void HandleEvents()
    127 {
    128     //Our SDL event placeholder.
    129     SDL_Event event;
    130     //Grab all the events off the queue.
    131     while(SDL_PollEvent(&event)) {
    132         switch(event.type) {
    133         case SDL_KEYDOWN:
    134             //Handle key Event
    135             HandleKeyEvent(&event.key.keysym);
    136             break;
    137         case SDL_QUIT:
    138             //Handle quit requests (like Ctrl-c).
    139             Quit(0);
    140             break;
    141         }
    142     }
    143 }
    144 
    145 int main(int argc, char* argv[])
    146 {
    147     printf("可以通过↑↓←→+ -按键控制移动和大小
    ");
    148     if (InitView(w, h, "yp.ico") == false) {
    149         SDL_Log("sdlWindow is null @_@
    ");
    150         Quit(0);
    151         return -1;
    152     }
    153 
    154     char *imgName = "gril.jpg";
    155     if (InitDraw(imgName) == false) {
    156         SDL_Log("Init Fail @_@
    ");
    157         Quit(0);
    158         return -1;
    159     }
    160     
    161     //配置客户区大小
    162     SDL_QueryTexture(sdlTexture,NULL, NULL, &w, &h);
    163     SDL_SetWindowSize(sdlWindow, w, h);
    164     SDL_Log("w=%d, h=%d
    ", w, h);
    165     
    166     while (1) {
    167         HandleEvents();
    168         UpdateDraw();
    169     }
    170     
    171     SDL_DestroyWindow(sdlWindow);
    172     SDL_Quit();
    173     return 0;
    174 }

    结果:

  • 相关阅读:
    移动web图片高度自适应的解决方案
    高性能滚动 scroll 及页面渲染优化
    Web学习之跨域问题及解决方案
    apply和call用法
    ES6学习之箭头函数
    JS 对象封装的常用方式
    为Array对象添加一个去除重复项的方法
    最全的常用正则表达式大全
    前端如何实现图片懒加载(lazyload) 提高用户体验
    菜鸟教程 Missing parentheses in call to 'print'
  • 原文地址:https://www.cnblogs.com/1024Planet/p/5752648.html
Copyright © 2011-2022 走看看