refactored to do drawing from a runloop action

This commit is contained in:
Bob Polis 2020-10-25 20:31:59 +01:00
parent 2479fd6d12
commit 30aad07c9f

117
main.cpp
View File

@ -15,6 +15,14 @@
#include <unistd.h> #include <unistd.h>
#include <cairo/cairo.h> #include <cairo/cairo.h>
const int WIDTH {700};
const int HEIGHT {700};
sc::gui::Window* main_window {nullptr};
sc::gui::Image* main_image {nullptr};
cairo_t* cr {nullptr};
bool timer_expired {true};
void print_help() { void print_help() {
std::cout << "usage: screensaver [-h|--version]\n"; std::cout << "usage: screensaver [-h|--version]\n";
std::cout << " -h, --help show this help text and exit\n"; std::cout << " -h, --help show this help text and exit\n";
@ -25,55 +33,40 @@ void print_version() {
std::cout << "screensaver version 1.0\n"; std::cout << "screensaver version 1.0\n";
} }
void draw(sc::gui::Window& window) { void draw() {
SDL_Surface* s {SDL_CreateRGBSurface(0, 700, 700, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0)}; if (timer_expired) {
sc::gui::Image image {s}; timer_expired = false;
sc::gui::ImageLock lock {image}; sc::gui::ImageLock lock {*main_image};
cairo_surface_t* cs {cairo_image_surface_create_for_data(static_cast<unsigned char*>(s->pixels),
CAIRO_FORMAT_RGB24, s->w, s->h, s->pitch)};
cairo_t* cr {cairo_create(cs)};
// white background // white background
cairo_set_source_rgb(cr, 1, 1, 1); cairo_set_source_rgb(cr, 1, 1, 1);
cairo_rectangle(cr, 0, 0, s->w, s->h); cairo_rectangle(cr, 0, 0, WIDTH, HEIGHT);
cairo_fill(cr); cairo_fill(cr);
// orange rectangle // orange rectangle
cairo_rectangle(cr, 300, 300, 100, 100); cairo_rectangle(cr, 300, 300, 100, 100);
cairo_set_source_rgb(cr, 1, 0.5, 0); cairo_set_source_rgb(cr, 1, 0.5, 0);
cairo_fill(cr); cairo_fill(cr);
// black rectangle // black rectangle
cairo_rectangle(cr, 200, 200, 100, 100); cairo_rectangle(cr, 200, 200, 100, 100);
cairo_set_source_rgb(cr, 0, 0, 0); cairo_set_source_rgb(cr, 0, 0, 0);
cairo_fill(cr); cairo_fill(cr);
// dark green text // dark green text
cairo_set_source_rgb(cr, 0.2, 0.5, 0); cairo_set_source_rgb(cr, 0.2, 0.5, 0);
cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 42); cairo_set_font_size(cr, 42);
cairo_move_to(cr, 100, 150); cairo_move_to(cr, 50, 100);
cairo_show_text(cr, "Cairo on SDL"); cairo_show_text(cr, "abcdefghijklmnopqrstuvwxyz");
// show // show
window.show_image(image); main_window->show_image(*main_image);
cairo_surface_destroy(cs); }
cairo_destroy(cr);
} }
int main(int argc, const char * argv[]) { int main(int argc, const char * argv[]) {
try { try {
// RAII instances
sc::gui::SDLWrapper sdl2;
sc::gui::SDLImageWrapper sdl_image;
// main window
sc::gui::Window& window {sc::gui::Window::new_window("screensaver")};
window.add_event_handler([](const SDL_Event& event, bool quit) -> bool {
std::cerr << "closing window " << event.window.windowID << '\n';
return quit;
}, SDL_WINDOWEVENT_CLOSE);
int opt_char, opt_val; int opt_char, opt_val;
struct option long_options[] = { struct option long_options[] = {
{"help", no_argument, nullptr, 'h'}, {"help", no_argument, nullptr, 'h'},
@ -99,20 +92,42 @@ int main(int argc, const char * argv[]) {
throw std::runtime_error("unrecognized option"); throw std::runtime_error("unrecognized option");
} }
} }
if (optind == argc) { // if (optind == argc) {
// here when no file args // // here when no file args
} // }
for (int i = optind; i < argc; ++i) { // for (int i = optind; i < argc; ++i) {
try { // try {
// process file argv[i] // // process file argv[i]
} catch (const std::runtime_error& ex) { // } catch (const std::runtime_error& ex) {
std::cerr << "screensaver: " << ex.what() << '\n'; // std::cerr << "screensaver: " << ex.what() << '\n';
} // }
} // }
draw(window); // RAII instances
sc::gui::SDLWrapper sdl2;
sc::gui::SDLImageWrapper sdl_image;
// main window
sc::gui::Window& window {sc::gui::Window::new_window("screensaver")};
main_window = &window;
window.add_event_handler([](const SDL_Event& event, bool quit) -> bool {
std::cerr << "closing window " << event.window.windowID << '\n';
return quit;
}, SDL_WINDOWEVENT_CLOSE);
SDL_Surface* s {SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0)};
sc::gui::Image image {s};
main_image = &image;
cairo_surface_t* cs {cairo_image_surface_create_for_data(static_cast<unsigned char*>(s->pixels),
CAIRO_FORMAT_RGB24, s->w, s->h, s->pitch)};
cr = cairo_create(cs);
sc::gui::app().add_run_loop_action(draw);
sc::gui::app().run(); sc::gui::app().run();
cairo_surface_destroy(cs);
cairo_destroy(cr);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
std::cerr << "screensaver: " << ex.what() << '\n'; std::cerr << "screensaver: " << ex.what() << '\n';
return EXIT_FAILURE; return EXIT_FAILURE;