118 lines
3.4 KiB
C++
118 lines
3.4 KiB
C++
//
|
|
// main.cpp
|
|
// screensaver
|
|
//
|
|
// Created by Bob Polis at 2020-10-23
|
|
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
|
//
|
|
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
#include <getopt.h>
|
|
#include <libscgui.hpp>
|
|
#include <unistd.h>
|
|
#include <cairo/cairo.h>
|
|
|
|
void print_help() {
|
|
std::cout << "usage: screensaver [-h|--version]\n";
|
|
std::cout << " -h, --help show this help text and exit\n";
|
|
std::cout << " --version show version number and exit\n";
|
|
}
|
|
|
|
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)};
|
|
|
|
// white background
|
|
cairo_set_source_rgb(cr, 1, 1, 1);
|
|
cairo_rectangle(cr, 0, 0, s->w, s->h);
|
|
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);
|
|
|
|
// light blue text
|
|
cairo_set_source_rgb(cr, 0, 0.5, 1);
|
|
cairo_select_font_face(cr, "Optima", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
|
|
cairo_set_font_size(cr, 42);
|
|
cairo_move_to(cr, 350, 150);
|
|
cairo_show_text(cr, "Cairo on SDL");
|
|
|
|
// show
|
|
window.show_image(image);
|
|
cairo_surface_destroy(cs);
|
|
cairo_destroy(cr);
|
|
}
|
|
|
|
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")};
|
|
|
|
int opt_char, opt_val;
|
|
struct option long_options[] = {
|
|
{"help", no_argument, nullptr, 'h'},
|
|
{"version", no_argument, &opt_val, 1},
|
|
{nullptr, 0, nullptr, 0}
|
|
};
|
|
while ((opt_char = getopt_long(argc, const_cast<char* const *>(argv), "h", long_options, nullptr)) != -1) {
|
|
std::string arg {optarg ? optarg : ""};
|
|
switch (opt_char) {
|
|
case 0: {
|
|
// handle long-only options here
|
|
switch (opt_val) {
|
|
case 1:
|
|
print_version();
|
|
return EXIT_SUCCESS;
|
|
}
|
|
break;
|
|
}
|
|
case 'h':
|
|
print_help();
|
|
return EXIT_SUCCESS;
|
|
case '?':
|
|
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';
|
|
}
|
|
}
|
|
|
|
draw(window);
|
|
sc::gui::app().run();
|
|
|
|
} catch (const std::exception& ex) {
|
|
std::cerr << "screensaver: " << ex.what() << '\n';
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|