void shutdown(void) { SGE_ASSERT(details::g_window != NULL); SGE_ASSERT(details::g_gl_context != NULL); SGE_ASSERT(s_imgui_context != NULL); shutdown_test(); s_program.Destroy(); ImGui::SetCurrentContext(s_imgui_context); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplSDL2_Shutdown(); ImGui::SetCurrentContext(NULL); ImGui::DestroyContext(s_imgui_context); s_imgui_context = NULL; if (details::g_gl_context == SDL_GL_GetCurrentContext()) SDL_GL_MakeCurrent(details::g_window, NULL); SDL_GL_DeleteContext(details::g_gl_context); details::g_gl_context = NULL; SDL_DestroyWindow(details::g_window); details::g_window = NULL; details::g_window_id = 0; }
bool begin(void) { SGE_ASSERT(details::g_window != NULL); SGE_ASSERT(details::g_gl_context != NULL); SGE_ASSERT(s_imgui_context != NULL); if (!details::g_window_visibled) return false; ImGui::SetCurrentContext(s_imgui_context); ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplSDL2_NewFrame(details::g_window); ImGui::NewFrame(); return true; }
SpriteComponent::SpriteComponent(Entity* ent) : RenderComponent(ent), color(1.0f), texture(nullptr), pipeline(nullptr) { transform = getParent()->getComponent<TransformComponent>(); key.fields.translucent = (color.a < 1.0f) ? 1 : 0; SGE_ASSERT(transform); }
SpriteComponent::SpriteComponent(Entity* ent, sge::Texture* texture, const sge::math::vec4& col) : RenderComponent(ent), color(col), texture(texture), pipeline(nullptr) { transform = getParent()->getComponent<TransformComponent>(); key.fields.translucent = (color.a < 1.0f) ? 1 : 0; SGE_ASSERT(transform); }
void end(void) { SGE_ASSERT(details::g_window != NULL); SGE_ASSERT(details::g_gl_context != NULL); ImGui::Render(); SDL_GL_MakeCurrent(details::g_window, details::g_gl_context); glViewport(0, 0, details::g_window_rect[2], details::g_window_rect[3]); glClear(GL_COLOR_BUFFER_BIT); s_program.Use(); commit_uniforms(); draw_test(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui::SetCurrentContext(NULL); SDL_GL_SwapWindow(details::g_window); }
bool handle_event(const SDL_Event &event) { SGE_ASSERT(details::g_window != NULL); SGE_ASSERT(s_imgui_context != NULL); ImGui::SetCurrentContext(s_imgui_context); ImGui_ImplSDL2_ProcessEvent(&event); ImGui::SetCurrentContext(NULL); if (event.type != SDL_WINDOWEVENT) return false; if (event.window.windowID != details::g_window_id) return false; switch (event.window.event) { case SDL_WINDOWEVENT_MOVED: details::g_window_rect[0] = event.window.data1; details::g_window_rect[1] = event.window.data2; return true; case SDL_WINDOWEVENT_RESIZED: details::g_window_rect[2] = event.window.data1; details::g_window_rect[3] = event.window.data2; return true; case SDL_WINDOWEVENT_EXPOSED: case SDL_WINDOWEVENT_SHOWN: details::g_window_visibled = true; return true; case SDL_WINDOWEVENT_HIDDEN: case SDL_WINDOWEVENT_MINIMIZED: details::g_window_visibled = false; return true; } return false; }
/****** sge_var/var_list_parse_from_string() ******************************* * NAME * var_list_parse_from_string() -- parse vars from string list * * SYNOPSIS * int var_list_parse_from_string(lList **lpp, * const char *variable_str, * int check_environment) * * FUNCTION * Parse a list of variables ("lpp") from a comma separated * string list ("variable_str"). The boolean "check_environment" * defined wether the current value of a variable is taken from * the environment of the calling process. * * INPUTS * lList **lpp - VA_Type list * const char *variable_str - source string * int check_environment - boolean * * RESULT * int - error state * 0 - OK * >0 - Error * * NOTES * MT-NOTE: var_list_parse_from_string() is MT safe *******************************************************************************/ int var_list_parse_from_string(lList **lpp, const char *variable_str, int check_environment) { char *variable; char *val_str; int var_len; char **str_str; char **pstr; lListElem *ep; char *va_string; DENTER(TOP_LAYER, "var_list_parse_from_string"); if (!lpp) { DEXIT; return 1; } va_string = sge_strdup(NULL, variable_str); if (!va_string) { *lpp = NULL; DEXIT; return 2; } str_str = string_list(va_string, ",", NULL); if (!str_str || !*str_str) { *lpp = NULL; sge_free(&va_string); DEXIT; return 3; } if (!*lpp) { *lpp = lCreateList("variable list", VA_Type); if (!*lpp) { sge_free(&va_string); sge_free(&str_str); DEXIT; return 4; } } for (pstr = str_str; *pstr; pstr++) { struct saved_vars_s *context; ep = lCreateElem(VA_Type); /* SGE_ASSERT(ep); */ lAppendElem(*lpp, ep); context = NULL; variable = sge_strtok_r(*pstr, "=", &context); SGE_ASSERT((variable)); var_len=strlen(variable); lSetString(ep, VA_variable, variable); val_str=*pstr; /* * The character at the end of the first token must be either '=' or '\0'. * If it's a '=' then we treat the following string as the value * If it's a '\0' and check_environment is set, then we get the value from * the environment variable value. * If it's a '\0' and check_environment is not set, then we set the value * to NULL. */ if (val_str[var_len] == '=') { lSetString(ep, VA_value, &val_str[var_len+1]); } else if (check_environment) { lSetString(ep, VA_value, sge_getenv(variable)); } else { lSetString(ep, VA_value, NULL); } sge_free_saved_vars(context); } sge_free(&va_string); sge_free(&str_str); DRETURN(0); }
void SpriteComponent::render(GraphicsDevice* device) { SGE_ASSERT(renderer); renderer->renderSprite(this); }
bool init(void) { SGE_ASSERT(details::g_window == NULL); SGE_ASSERT(details::g_gl_context == NULL); SGE_LOGD("Creating OpenGL window...\n"); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); #ifdef SGE_DEBUG SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); #endif details::g_window = SDL_CreateWindow("SGE", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI); if (details::g_window == NULL) return false; details::g_window_id = SDL_GetWindowID(details::g_window); SDL_GetWindowPosition(details::g_window, &details::g_window_rect[0], &details::g_window_rect[1]); SDL_GetWindowSize(details::g_window, &details::g_window_rect[2], &details::g_window_rect[3]); SGE_LOGD("Initializing OpenGL...\n"); details::g_gl_context = SDL_GL_CreateContext(details::g_window); if (details::g_gl_context == NULL) { SGE_LOGE("Failed to create OpenGL context.\n"); SDL_DestroyWindow(details::g_window); details::g_window = NULL; return false; } SDL_GL_MakeCurrent(details::g_window, details::g_gl_context); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { SGE_LOGE("Failed to initialize GLEW.\n"); SDL_GL_DeleteContext(details::g_gl_context); details::g_gl_context = NULL; SDL_DestroyWindow(details::g_window); details::g_window = NULL; return false; } #ifdef SGE_DEBUG if (GL_KHR_debug) { SGE_LOGD("Enable OpenGL debug output.\n"); glDebugMessageCallback(gl_debug_output, NULL); } #endif SGE_LOGI("GLEW: %s\n", glewGetString(GLEW_VERSION)); SGE_LOGI("OpenGL: %s\n", glGetString(GL_VERSION)); IMGUI_CHECKVERSION(); s_imgui_context = ImGui::CreateContext(); if (s_imgui_context == NULL) { SGE_LOGE("Failed to initialize ImGui.\n"); SDL_GL_DeleteContext(details::g_gl_context); details::g_gl_context = NULL; SDL_DestroyWindow(details::g_window); details::g_window = NULL; return false; } ImGui::SetCurrentContext(s_imgui_context); ImGuiIO &io = ImGui::GetIO(); io.IniFilename = NULL; SGE_LOGI("ImGui: %s\n", ImGui::GetVersion()); ImGui::StyleColorsDark(); ImGuiStyle &style = ImGui::GetStyle(); style.Alpha = 1.1f; style.WindowRounding = 0.0f; style.WindowBorderSize = 1.0f; style.ChildRounding = 0.0f; style.ChildBorderSize = 0.0f; style.PopupRounding = 0.0f; style.PopupBorderSize = 0.0f; ImGui_ImplSDL2_InitForOpenGL(details::g_window, details::g_gl_context); ImGui_ImplOpenGL3_Init("#version 130"); if (!init_program()) { SGE_LOGE("Failed to initialize ImGui.\n"); SDL_GL_DeleteContext(details::g_gl_context); details::g_gl_context = NULL; SDL_DestroyWindow(details::g_window); details::g_window = NULL; return false; } s_uniform_mat4[UNIFORM_MODEL_MATRIX] = glm::mat4(1.0f); s_uniform_mat4[UNIFORM_VIEW_MATRIX] = glm::mat4(1.0f); s_uniform_mat4[UNIFORM_PROJECTION_MATRIX] = glm::frustum(-1.0f, 1.0f, -1.0f, 1.0f, 0.5f, 999.0f); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); init_test(); return true; }
const std::vector<char>& ShaderResource::loadShader() { std::ifstream file; if (resourcePath.find(".glsl") != std::string::npos) { file.open(resourcePath, std::ios::in); if (file.is_open()) { std::cout << "SUCCESS: Opened shader " << resourcePath << std::endl; std::stringstream stream; std::string str; stream << file.rdbuf(); str = stream.str(); std::copy(str.begin(), str.end(), std::back_inserter(data)); data.push_back('\0'); file.close(); } else { std::cout << "ERROR: Could not open shader: " << resourcePath << std::endl; SGE_ASSERT(false); } } else if (resourcePath.find(".cso") != std::string::npos) { file.open(resourcePath, std::ios::in | std::ios::ate | std::ios::binary); if (file.is_open()) { std::cout << "SUCCESS: Opened shader " << resourcePath << std::endl; data.resize(static_cast<size_t>(file.tellg())); file.seekg(0, std::ios::beg); file.read(data.data(), data.size()); file.close(); } else { std::cout << "ERROR: Could not open shader: " << resourcePath << std::endl; SGE_ASSERT(false); } } else { std::cout << "ERROR: Could not open shader: " << resourcePath << std::endl; SGE_ASSERT(false); } return data; }