Last active
January 10, 2021 19:15
-
-
Save lethal-guitar/f8c1625198acbd08304a668e3057b5c2 to your computer and use it in GitHub Desktop.
Fullscreen text viewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Copyright (c) 2021 Nikolai Wuttke | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
#include "imgui.h" | |
#include "imgui_impl_sdl.h" | |
#include "imgui_impl_opengl3.h" | |
#include <SDL.h> | |
#include <GLES2/gl2.h> | |
#include <stdio.h> | |
#include <fstream> | |
int main(int argc, char** argv) | |
{ | |
if (argc <= 1) | |
{ | |
printf("No input given\n"); | |
return 0; | |
} | |
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) | |
{ | |
printf("Error: %s\n", SDL_GetError()); | |
return -1; | |
} | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); | |
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); | |
SDL_DisplayMode displayMode; | |
SDL_GetDesktopDisplayMode(0, &displayMode); | |
SDL_Window* window = SDL_CreateWindow( | |
"Log Viewer", | |
SDL_WINDOWPOS_CENTERED, | |
SDL_WINDOWPOS_CENTERED, | |
displayMode.w, | |
displayMode.h, | |
SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI); | |
SDL_GLContext gl_context = SDL_GL_CreateContext(window); | |
SDL_GL_MakeCurrent(window, gl_context); | |
SDL_GL_SetSwapInterval(1); // Enable vsync | |
// Setup Dear ImGui context | |
IMGUI_CHECKVERSION(); | |
ImGui::CreateContext(); | |
ImGuiIO& io = ImGui::GetIO(); | |
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; | |
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; | |
// Setup Dear ImGui style | |
ImGui::StyleColorsDark(); | |
//ImGui::StyleColorsClassic(); | |
// Setup Platform/Renderer bindings | |
ImGui_ImplSDL2_InitForOpenGL(window, gl_context); | |
ImGui_ImplOpenGL3_Init(nullptr); | |
const char* inputFilename = argv[1]; | |
std::string inputText; | |
{ | |
std::ifstream file(inputFilename, std::ios::ate); | |
const size_t fileSize = file.tellg(); | |
file.seekg(0); | |
inputText.resize(fileSize); | |
file.read(&inputText[0], fileSize); | |
} | |
// Main loop | |
bool running = true; | |
while (running) | |
{ | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) | |
{ | |
ImGui_ImplSDL2_ProcessEvent(&event); | |
if ( | |
event.type == SDL_QUIT || | |
(event.type == SDL_WINDOWEVENT && | |
event.window.event == SDL_WINDOWEVENT_CLOSE && | |
event.window.windowID == SDL_GetWindowID(window)) || | |
(event.type == SDL_CONTROLLERBUTTONDOWN && | |
event.cbutton.button == SDL_CONTROLLER_BUTTON_B) | |
) { | |
running = false; | |
} | |
} | |
// Start the Dear ImGui frame | |
ImGui_ImplOpenGL3_NewFrame(); | |
ImGui_ImplSDL2_NewFrame(window); | |
ImGui::NewFrame(); | |
// Draw single window with scrollable text | |
const ImVec2 windowSize = io.DisplaySize; | |
ImGui::SetNextWindowSize(windowSize); | |
ImGui::SetNextWindowPos(ImVec2(0, 0)); | |
ImGui::Begin( | |
inputFilename, | |
&running, | |
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize); | |
ImGui::TextUnformatted(inputText.c_str()); | |
ImGui::End(); | |
// Rendering | |
ImGui::Render(); | |
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | |
SDL_GL_SwapWindow(window); | |
} | |
// Cleanup | |
ImGui_ImplOpenGL3_Shutdown(); | |
ImGui_ImplSDL2_Shutdown(); | |
ImGui::DestroyContext(); | |
SDL_GL_DeleteContext(gl_context); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Cross Platform Makefile | |
# Compatible with MSYS2/MINGW, Ubuntu 14.04.1 and Mac OS X | |
# | |
# You will need SDL2 (http://www.libsdl.org): | |
# Linux: | |
# apt-get install libsdl2-dev | |
# Mac OS X: | |
# brew install sdl2 | |
# MSYS2: | |
# pacman -S mingw-w64-i686-SDL2 | |
# | |
#CXX = g++ | |
#CXX = clang++ | |
EXE = example_sdl_opengl3 | |
IMGUI_DIR = ../.. | |
SOURCES = main.cpp | |
SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp | |
SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl.cpp $(IMGUI_DIR)/backends/imgui_impl_opengl3.cpp | |
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) | |
UNAME_S := $(shell uname -s) | |
CXXFLAGS = -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends | |
CXXFLAGS += -g -Wall -Wformat | |
LIBS = | |
##--------------------------------------------------------------------- | |
## OPENGL LOADER | |
##--------------------------------------------------------------------- | |
CXXFLAGS += -DIMGUI_IMPL_OPENGL_ES2 | |
LIBS += -lGLESv2 | |
## Using OpenGL loader: gl3w [default] | |
# SOURCES += ../libs/gl3w/GL/gl3w.c | |
# CXXFLAGS += -I../libs/gl3w -DIMGUI_IMPL_OPENGL_LOADER_GL3W | |
## Using OpenGL loader: glew | |
## (This assumes a system-wide installation) | |
# CXXFLAGS += -DIMGUI_IMPL_OPENGL_LOADER_GLEW | |
# LIBS += -lGLEW | |
## Using OpenGL loader: glad | |
# SOURCES += ../libs/glad/src/glad.c | |
# CXXFLAGS += -I../libs/glad/include -DIMGUI_IMPL_OPENGL_LOADER_GLAD | |
## Using OpenGL loader: glad2 | |
# SOURCES += ../libs/glad/src/gl.c | |
# CXXFLAGS += -I../libs/glad/include -DIMGUI_IMPL_OPENGL_LOADER_GLAD2 | |
## Using OpenGL loader: glbinding | |
## This assumes a system-wide installation | |
## of either version 3.0.0 (or newer) | |
# CXXFLAGS += -DIMGUI_IMPL_OPENGL_LOADER_GLBINDING3 | |
# LIBS += -lglbinding | |
## or the older version 2.x | |
# CXXFLAGS += -DIMGUI_IMPL_OPENGL_LOADER_GLBINDING2 | |
# LIBS += -lglbinding | |
##--------------------------------------------------------------------- | |
## BUILD FLAGS PER PLATFORM | |
##--------------------------------------------------------------------- | |
ifeq ($(UNAME_S), Linux) #LINUX | |
ECHO_MESSAGE = "Linux" | |
LIBS += -lGL -ldl `sdl2-config --libs` | |
CXXFLAGS += `sdl2-config --cflags` | |
CFLAGS = $(CXXFLAGS) | |
endif | |
ifeq ($(UNAME_S), Darwin) #APPLE | |
ECHO_MESSAGE = "Mac OS X" | |
LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo `sdl2-config --libs` | |
LIBS += -L/usr/local/lib -L/opt/local/lib | |
CXXFLAGS += `sdl2-config --cflags` | |
CXXFLAGS += -I/usr/local/include -I/opt/local/include | |
CFLAGS = $(CXXFLAGS) | |
endif | |
ifeq ($(findstring MINGW,$(UNAME_S)),MINGW) | |
ECHO_MESSAGE = "MinGW" | |
LIBS += -lgdi32 -lopengl32 -limm32 `pkg-config --static --libs sdl2` | |
CXXFLAGS += `pkg-config --cflags sdl2` | |
CFLAGS = $(CXXFLAGS) | |
endif | |
##--------------------------------------------------------------------- | |
## BUILD RULES | |
##--------------------------------------------------------------------- | |
%.o:%.cpp | |
$(CXX) $(CXXFLAGS) -c -o $@ $< | |
%.o:$(IMGUI_DIR)/%.cpp | |
$(CXX) $(CXXFLAGS) -c -o $@ $< | |
%.o:$(IMGUI_DIR)/backends/%.cpp | |
$(CXX) $(CXXFLAGS) -c -o $@ $< | |
%.o:../libs/gl3w/GL/%.c | |
$(CC) $(CFLAGS) -c -o $@ $< | |
%.o:../libs/glad/src/%.c | |
$(CC) $(CFLAGS) -c -o $@ $< | |
all: $(EXE) | |
@echo Build complete for $(ECHO_MESSAGE) | |
$(EXE): $(OBJS) | |
$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS) | |
clean: | |
rm -f $(EXE) $(OBJS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment