This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
example.cpp
86 lines (65 loc) · 2.08 KB
/
example.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "SDL.h"
#undef main
#include "imgui.h"
#include "imgui_sdl.h"
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("SDL2 ImGui Renderer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_RESIZABLE);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
ImGui::CreateContext();
ImGuiSDL::Initialize(renderer, 800, 600);
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 100, 100);
{
SDL_SetRenderTarget(renderer, texture);
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255);
SDL_RenderClear(renderer);
SDL_SetRenderTarget(renderer, nullptr);
}
bool run = true;
while (run)
{
ImGuiIO& io = ImGui::GetIO();
int wheel = 0;
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT) run = false;
else if (e.type == SDL_WINDOWEVENT)
{
if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
io.DisplaySize.x = static_cast<float>(e.window.data1);
io.DisplaySize.y = static_cast<float>(e.window.data2);
}
}
else if (e.type == SDL_MOUSEWHEEL)
{
wheel = e.wheel.y;
}
}
int mouseX, mouseY;
const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
// Setup low-level inputs (e.g. on Win32, GetKeyboardState(), or write to those fields from your Windows message loop handlers, etc.)
io.DeltaTime = 1.0f / 60.0f;
io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
io.MouseWheel = static_cast<float>(wheel);
ImGui::NewFrame();
ImGui::ShowDemoWindow();
ImGui::Begin("Image");
ImGui::Image(texture, ImVec2(100, 100));
ImGui::End();
SDL_SetRenderDrawColor(renderer, 114, 144, 154, 255);
SDL_RenderClear(renderer);
ImGui::Render();
ImGuiSDL::Render(ImGui::GetDrawData());
SDL_RenderPresent(renderer);
}
ImGuiSDL::Deinitialize();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
ImGui::DestroyContext();
return 0;
}