码迷,mamicode.com
首页 > 其他好文 > 详细

SDL绘制简单几何图形

时间:2015-07-27 00:23:38      阅读:425      评论:0      收藏:0      [点我收藏+]

标签:sdl2

用到的API

  • SDL_SetRenderDrawColor(SDL_Renderer* renderer, Uint8 r,
    Uint8 g, Uint8 b, Uint8 a)
  • SDL_RenderClear(SDL_Renderer* renderer)
  • SDL_RenderFillRect(SDL_Renderer* renderer, const SDL_Rect *rect)
  • SDL_RenderDrawRect(SDL_Renderer* renderer, const SDL_Rect *rect)
  • SDL_RenderDrawLine(SDL_Renderer* renderer,int x1,int y1,int x2,int y2)
  • SDL_RenderDrawPoint(SDL_Renderer* renderer,int x,int y)
  • SDL_RenderPresent(SDL_Renderer* renderer);

API介绍

  • SDL_SetRenderDrawColor 接收的四个参数分别是RGB通道和alpha通道
  • SDL_RenderClear 用设置的绘画颜色清理目标渲染器
  • SDL_RenderFillRect 用设置的绘画颜色填充矩形
  • SDL_RenderDrawRect 用设置的绘画颜色绘制矩形边框
  • SDL_RenderDrawLine 用设置的绘画颜色绘制线段
  • SDL_RenderDrawPoint 用设置的绘画颜色绘制点
  • SDL_RenderPresent 用渲染器中的内容更新屏幕

SDL_Rect 介绍

SDL_Rect 是一个结构体,其中有四个int型的成员,x,y,w,h
x,y 为坐标, w,h 为宽和高
SDL中的x,y坐标系如图片所示
技术分享

代码

#include <SDL.h>

#define SCREEN_W 800
#define SCREEN_H 600

int main(int argc, char **argv) {
    SDL_Window *pWindow = NULL;
    SDL_Renderer *pRenderer = NULL;
    SDL_Init(SDL_INIT_EVERYTHING);
    pWindow = SDL_CreateWindow("Geometry Rendering",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_W,SCREEN_H,SDL_WINDOW_OPENGL);
    pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    bool quit = false;
    SDL_Event e;
    while( !quit ) {
        while( SDL_PollEvent(&e) != 0 ) {
            if(e.type == SDL_QUIT)
                quit = true;
        }
        SDL_SetRenderDrawColor(pRenderer,0xFF,0xFF,0xFF,0xFF);
        SDL_RenderClear(pRenderer);

        SDL_Rect fillRect = {SCREEN_W / 4, SCREEN_H / 4, SCREEN_W / 2, SCREEN_H / 2};   
        SDL_SetRenderDrawColor(pRenderer, 0xFF, 0x00, 0x00, 0xFF);
        SDL_RenderFillRect(pRenderer, &fillRect);       //填充矩形

        SDL_Rect outlineRect = {SCREEN_W / 6, SCREEN_H / 6, SCREEN_W * 2 / 3, SCREEN_H * 2 / 3};
        SDL_SetRenderDrawColor(pRenderer,0x00,0xFF,0x00,0xFF);
        SDL_RenderDrawRect(pRenderer,&outlineRect);     //绘制矩形边框

        SDL_SetRenderDrawColor(pRenderer,0x00,0x00,0xFF,0xFF);
        SDL_RenderDrawLine(pRenderer,SCREEN_W * 4 / 5,SCREEN_H * 4 / 5,SCREEN_W * 1 / 5,SCREEN_H * 1 / 5);  //绘制线段

        SDL_SetRenderDrawColor(pRenderer,0xFF,0xFF,0x00,0xFF);
        for(int i = 0; i <= SCREEN_H; i += 10) {
            SDL_RenderDrawPoint(pRenderer, SCREEN_W / 2, i);    //绘制点
        }


        SDL_RenderPresent(pRenderer);
    }

    SDL_DestroyWindow(pWindow);
    SDL_DestroyRenderer(pRenderer);
    SDL_Quit();
    return 0;
}

效果

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

SDL绘制简单几何图形

标签:sdl2

原文地址:http://blog.csdn.net/abang1996/article/details/47073239

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!