-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestSDL.cpp
More file actions
60 lines (51 loc) · 1.65 KB
/
testSDL.cpp
File metadata and controls
60 lines (51 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char ** argv)
{
bool leftMouseButtonDown = false;
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * window = SDL_CreateWindow("SDL2 Pixel Drawing",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Texture * texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 640, 480);
Uint32 * pixels = new Uint32[640 * 480];
memset(pixels, 255, 640 * 480 * sizeof(Uint32));
while (!quit)
{
SDL_UpdateTexture(texture, NULL, pixels, 640 * sizeof(Uint32));
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
leftMouseButtonDown = false;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
leftMouseButtonDown = true;
case SDL_MOUSEMOTION:
if (leftMouseButtonDown)
{
int mouseX = event.motion.x;
int mouseY = event.motion.y;
pixels[mouseY * 640 + mouseX] = 0;
}
break;
case SDL_QUIT:
quit = true;
break;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
delete[] pixels;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}