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 <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() {
std::cout << "usage: screensaver [-h|--version]\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";
}
void draw(sc::gui::Window& window) {
SDL_Surface* s {SDL_CreateRGBSurface(0, 700, 700, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0)};
sc::gui::Image image {s};
sc::gui::ImageLock lock {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)};
void draw() {
if (timer_expired) {
timer_expired = false;
sc::gui::ImageLock lock {*main_image};
// white background
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_rectangle(cr, 0, 0, s->w, s->h);
cairo_fill(cr);
// white background
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_rectangle(cr, 0, 0, WIDTH, HEIGHT);
cairo_fill(cr);
// orange rectangle
cairo_rectangle(cr, 300, 300, 100, 100);
cairo_set_source_rgb(cr, 1, 0.5, 0);
cairo_fill(cr);
// orange rectangle
cairo_rectangle(cr, 300, 300, 100, 100);
cairo_set_source_rgb(cr, 1, 0.5, 0);
cairo_fill(cr);
// black rectangle
cairo_rectangle(cr, 200, 200, 100, 100);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_fill(cr);
// black rectangle
cairo_rectangle(cr, 200, 200, 100, 100);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_fill(cr);
// dark green text
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_set_font_size(cr, 42);
cairo_move_to(cr, 100, 150);
cairo_show_text(cr, "Cairo on SDL");
// dark green text
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_set_font_size(cr, 42);
cairo_move_to(cr, 50, 100);
cairo_show_text(cr, "abcdefghijklmnopqrstuvwxyz");
// show
window.show_image(image);
cairo_surface_destroy(cs);
cairo_destroy(cr);
// show
main_window->show_image(*main_image);
}
}
int main(int argc, const char * argv[]) {
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;
struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
@ -99,20 +92,42 @@ int main(int argc, const char * argv[]) {
throw std::runtime_error("unrecognized option");
}
}
if (optind == argc) {
// here when no file args
}
for (int i = optind; i < argc; ++i) {
try {
// process file argv[i]
} catch (const std::runtime_error& ex) {
std::cerr << "screensaver: " << ex.what() << '\n';
}
}
// if (optind == argc) {
// // here when no file args
// }
// for (int i = optind; i < argc; ++i) {
// try {
// // process file argv[i]
// } catch (const std::runtime_error& ex) {
// 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();
cairo_surface_destroy(cs);
cairo_destroy(cr);
} catch (const std::exception& ex) {
std::cerr << "screensaver: " << ex.what() << '\n';
return EXIT_FAILURE;