SDL2画点 随手记

来源:互联网 发布:芮纳纪 淘宝 编辑:程序博客网 时间:2024/05/24 06:43

SDL官网

http://www.libsdl.org/

参考链接

http://lazyfoo.net/tutorials/SDL/08_geometry_rendering/index.php


记录一下 SDL2画点的关键步骤 .做好手写光栅画渲染器的第一步

#include <iostream>#include "SDL.h"#include <stdio.h>const int SCREEN_WIDTH = 150;const int SCREEN_HEIGHT = 150;SDL_Window* gWindow = nullptr;SDL_Surface* gScreenSurface = nullptr;SDL_Surface* gHelloWorld = nullptr;SDL_Renderer* gRenderer = nullptr;bool init();bool loadMedia();void close();bool init(){if (SDL_Init(SDL_INIT_VIDEO) < 0){printf("SDL can not initialized!SDL Error:%s\n", SDL_GetError());return false;}gWindow = SDL_CreateWindow("msdl1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);if (gWindow == nullptr){printf("SDL can't create window!SDL Error:%s\n", SDL_GetError());return -1;}gScreenSurface = SDL_GetWindowSurface(gWindow);gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);if (gRenderer == nullptr){return false;}//SDL_FillRect(gScreenSurface, nullptr, SDL_MapRGBA(gScreenSurface->format, 0xff, 0xff, 0xff, 0xff));SDL_FillRect(gScreenSurface, nullptr, SDL_MapRGBA(gScreenSurface->format, 0, 0, 0, 0));return true;}void close(){SDL_FreeSurface(gHelloWorld);SDL_DestroyRenderer(gRenderer);SDL_DestroyWindow(gWindow);SDL_Quit();}bool loadMedia(){gHelloWorld = SDL_LoadBMP("test.bmp");if (gHelloWorld == nullptr){return false;}return true;}void mainLoop(){/*SDL_BlitSurface(gHelloWorld, nullptr, gScreenSurface, nullptr);SDL_UpdateWindowSurface(gWindow);*/bool quit = false;SDL_Event evt;while (!quit){while (SDL_PollEvent(&evt) != 0){if (evt.type == SDL_QUIT){quit = true;}}SDL_RenderClear(gRenderer);// bg{SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xff);SDL_Rect fillRect = { 0,0, SCREEN_WIDTH, SCREEN_HEIGHT};SDL_RenderFillRect(gRenderer, &fillRect);}/*// rect{SDL_SetRenderDrawColor(gRenderer, 0xff, 0xff, 0x00, 0xff);SDL_Rect fillRect = { SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 };SDL_RenderFillRect(gRenderer, &fillRect);}// point{SDL_SetRenderDrawColor(gRenderer, 0xff, 0x00, 0x0f, 0xff);for (int i = 0; i < SCREEN_HEIGHT; i += 4){SDL_RenderDrawPoint(gRenderer, SCREEN_WIDTH / 2, i);}}*/for (int row = 0; row < SCREEN_HEIGHT;row++){for (int col = 0; col < SCREEN_WIDTH;col++){int r = rand()/100 % 256;int g = rand() / 100 % 256;int b = rand() / 100 % 256;SDL_SetRenderDrawColor(gRenderer, (Uint8)r, (Uint8)g, (Uint8)b, 0xff);SDL_RenderDrawPoint(gRenderer, col,row);}}SDL_RenderPresent(gRenderer);}}int main(int argc,char* args[]){if (!init()) return -1;if (!loadMedia()) return -1;mainLoop();close();return 0;}


关键步骤:

1. sdl2 头文件,库文件,dll

2.创建 window SDL_CreateWindow()

3. 用window 创建  renderer SDL_CreateRenderer()

4. 绘制前 SDL_RenderClear(gRenderer); 清理 renderer

4. renderer 设置颜色,并绘制

SDL_SetRenderDrawColor(gRenderer, (Uint8)r, (Uint8)g, (Uint8)b, 0xff);
SDL_RenderDrawPoint(gRenderer, col,row);

5. 绘制后  SDL_RenderPresent()  把renderer 绘制的的内容展现出来 


现在可以操作窗口里的每个象素.搞 渲染器的 准备工作做好了,接下来需要啃就是图形学的干货




0 0
原创粉丝点击