/** * Creates a new fullscreen window. * * Updates the pointers with the window width, window height, and bar height. */ static Window *create_window(int *width, int *height) { const char *title = GAME_NAME; const int x = SDL_WINDOWPOS_CENTERED; const int y = SDL_WINDOWPOS_CENTERED; const int w = get_window_width(); const int h = get_window_height(); const Uint32 flags = SDL_WINDOW_INPUT_FOCUS; Window *window; window = SDL_CreateWindow(title, x, y, w, h, flags); SDL_GetWindowSize(window, width, height); return window; }
int main(void) { t_env *env; char buffer[20]; init_signals(); tgetent(0, getenv("TERM")); if (!(env = malloc(sizeof(*env)))) quit("Failed to malloc env"); if (!(env->caps = malloc(sizeof(*env->caps)))) quit("Failed to malloc env caps"); env->messages = NULL; if (!(env->input = ft_memalloc(1))) quit("Failed to malloc new input"); init_caps(env->caps); terminal_catch_mode(); ft_putstr(env->caps->fullscreen_start); ft_putstr(env->caps->clear); ft_putstr(env->caps->stand_start); ft_putstr(env->caps->bold_start); int i = 0; set_cursor_position(env, 0, get_window_height() - 2); int width = get_window_width(); while (i < width) { ft_putchar('-'); i++; } ft_putstr(env->caps->stand_end); fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); int t = 0; int rd; while (t < 50) { ft_bzero(buffer, 20); while ((rd = read(0, buffer, 20)) < 1) { if (errno != EAGAIN && errno != EWOULDBLOCK) quit("Error on stdin read"); //check tcp //check resize } ft_putstr(buffer); t++; } fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK); ft_putstr(env->caps->fullscreen_end); terminal_normal_mode(); return (0); }
/** * Prints the provided strings centered at the specified absolute line. */ Code print_centered_horizontally(const int y, const int string_count, char **strings, const ColorPair color_pair, Renderer *renderer) { char log_buffer[MAXIMUM_STRING_SIZE]; const SDL_Color foreground = to_sdl_color(color_pair.foreground); const SDL_Color background = to_sdl_color(color_pair.background); const int slice_size = get_window_width() / string_count; Font *font = global_monospaced_font; SDL_Surface *surface; SDL_Texture *texture; SDL_Rect position; int i; position.x = 0; position.y = y; /* Validate that x and y are nonnegative. */ if (y < 0) { return CODE_ERROR; } for (i = 0; i < string_count; i++) { if (*strings[i] == '\0') { continue; } surface = TTF_RenderText_Shaded(font, strings[i], foreground, background); if (surface == NULL) { sprintf(log_buffer, CREATE_SURFACE_FAIL, "print_centered_horizontally()"); log_message(log_buffer); return CODE_ERROR; } texture = SDL_CreateTextureFromSurface(renderer, surface); if (texture == NULL) { sprintf(log_buffer, CREATE_TEXTURE_FAIL, "print_centered_horizontally()"); log_message(log_buffer); return CODE_ERROR; } /* Copy destination width and height from the texture. */ SDL_QueryTexture(texture, NULL, NULL, &position.w, &position.h); position.x = i * slice_size + (slice_size - position.w) / 2; SDL_RenderCopy(renderer, texture, NULL, &position); SDL_DestroyTexture(texture); SDL_FreeSurface(surface); } return CODE_OK; }
void GameOfLife::on_init() { rect.set_color(glm::vec3(0.15f, 1.0f, 0.2f)); rect.set_size(glm::vec2(cell_size)); srand(static_cast<unsigned>(time(NULL))); width = std::ceil(get_window_width() / cell_size); height = std::ceil(get_window_height() / cell_size); map = new bool*[width]; prev_map = new bool*[width]; for(unsigned i = 0; i < width; ++i) { map[i] = new bool[height]; prev_map[i] = new bool[height]; for(unsigned j = 0; j < height; ++j) { map[i][j] = prev_map[i][j] = false;//!static_cast<bool>(rand()%3); } } cout << get_cell_neighbors(0, 0) << endl; }
/** * Prints the provided string after formatting it to increase readability. */ void print_long_text(char *string, Renderer *renderer) { char log_buffer[MAXIMUM_STRING_SIZE]; const int font_width = get_font_width(); const int width = get_window_width() - 2 * get_padding() * font_width; TTF_Font *font = get_font(); SDL_Surface *surface; SDL_Texture *texture; SDL_Color color = to_sdl_color(COLOR_DEFAULT_FOREGROUND); SDL_Rect position; position.x = get_padding() * font_width; position.y = get_padding() * font_width; clear(renderer); /* Validate that the string is not empty and that x and y are nonnegative. */ if (string == NULL || string[0] == '\0') { return; } remove_first_breaks(string); surface = TTF_RenderText_Blended_Wrapped(font, string, color, width); if (surface == NULL) { sprintf(log_buffer, CREATE_SURFACE_FAIL, "print_long_text()"); log_message(log_buffer); return; } texture = SDL_CreateTextureFromSurface(renderer, surface); if (texture == NULL) { sprintf(log_buffer, CREATE_TEXTURE_FAIL, "print_long_text()"); log_message(log_buffer); return; } /* Copy destination width and height from the texture. */ SDL_QueryTexture(texture, NULL, NULL, &position.w, &position.h); SDL_RenderCopy(renderer, texture, NULL, &position); SDL_DestroyTexture(texture); SDL_FreeSurface(surface); present(renderer); }
void mouse_moved( int x, int y) { usleep(100); camera_pose_t * camera_pose = get_camera_pose(); int window_width = get_window_width(); int window_height = get_window_height(); switch (mouse_rotation.button) { case GLUT_LEFT_BUTTON: // rotate { camera_pose->phi -= ((float)( mouse_rotation.x0 - x ))/((float)(window_width))*2.0*3.14; camera_pose->theta -= ((float)( mouse_rotation.y0 - y ))/((float)(window_height))*2.0*3.14; if (camera_pose->theta > 0.5*3.14) camera_pose->theta = 0.5*3.14; if (camera_pose->theta < -0.5*3.14f) camera_pose->theta = -0.5*3.14f; mouse_rotation.x0 = x; mouse_rotation.y0 = y; set_camera(); break; } case GLUT_RIGHT_BUTTON: // pan { camera_pose->focus_x += 500.0f*cos(camera_pose->phi)*((float)(y - mouse_rotation.y0))/((float)(window_height)); camera_pose->focus_y += 500.0f*sin(camera_pose->phi)*((float)(y - mouse_rotation.y0))/((float)(window_height));; camera_pose->focus_x += 500.0f*sin(camera_pose->phi)*((float)(x - mouse_rotation.x0))/((float)(window_width)); camera_pose->focus_y -= 500.0f*cos(camera_pose->phi)*((float)(x - mouse_rotation.x0))/((float)(window_width));; mouse_rotation.x0 = x; mouse_rotation.y0 = y; set_camera(); break; } /* case GLUT_MIDDLE_BUTTON: // zoom { camera_pose->rho += 1000.0f*((float)(y - mouse_rotation.y0))/((float)(window_height)); if (camera_pose->rho < 1.0f) camera_pose->rho = 1.0f; if (camera_pose->rho > 2000.0f) camera_pose->rho = 2000.0f; mouse_rotation.x0 = x; mouse_rotation.y0 = y; set_camera(); break; } */ case GLUT_MIDDLE_BUTTON: // move camera focus { camera_pose->focus_z += 500.0f*((float)(y - mouse_rotation.y0))/((float)(window_height)); if (camera_pose->focus_z > 0.0f) camera_pose->focus_z = 0.0f; mouse_rotation.x0 = x; mouse_rotation.y0 = y; set_camera(); break; } default: break; }// switch (mouse_rotation.button) }