Skip to content

Documentation

ahmad-PH edited this page Mar 27, 2019 · 42 revisions
Table of Contents

Point

A struct to store points and coordinates

Point

Point(int x, int y);

Create a Point object. Please note that the y coordinate has a different behaviour in the computer world, than in the math world. The y coordinates grows downwards in computer graphics; Meaning that if a point has a higher y, it will be drawn lower in the screen.

Example:

Point P(10, 20);

Rectangle

A struct representing a rectangle, mainly used to specify where images must be drawn.

Rectangle

Rectangle(int x, int y, int w, int h)

Constructs a rectangle with (x,y) as its top-left corner, w as its width, and h as its height.

Rectangle(Point top_left, int w, int h)

Constructs a rectangle with top_left as its top-left corner, w as its width, and h as its height. handy in case you already have a Point representing the top-left of the rectangle.

Rectangle(Point top_left, Point bottom_right)

Pretty self-explanatory! handy if you already have the two Points and don't want to unpack them.

RGB

A struct to store colors in Red-Green-Blue format

RGB

RGB(int r, int g, int b)

Create an RGB object.

Example:

RGB PERSIAN_BLUE(28, 57, 187);

Window

draw_img

void draw_img(string filename, Rectangle dst = NULL_RECT, Rectangle src = NULL_RECT, double angle = 0, bool flip_horizontal = false, bool flip_vertical = false);

Draws an image with the specified properties. dst specifies the part of the window on which the image will be drawn. src specifies the part of the image that you want cropped-out and drawn. If any of these Rectangle are the NULL_RECT, then the entire window/image will be selected. So if dst is NULL_RECT, then the image will be drawn over the entire window, and if src is NULL_RECT, then all of the image is selected (nothing is cropped out). angle is how much the picture must be rotated clockwise. The unit of measurement is degrees. flip_horizontal and flip_vertical indicate whether the image must be flipped horizontally or vertically, as their name suggests.

Example:

win.draw_bmp("myimage.bmp", Rectangle(0,0,200,200));

This will draw "my_image.bmp", on the top-left of the window in a rectangle of 200 width and height.

show_text

void show_text(string input, Point src, RGB color = WHITE, string fontAddress = "FreeSans.ttf", int size = 24);

Shows the input string starting from src with the specified color, font and size.

Example:

win.show_text("Hello", Point(100, 100), RED, "FreeSans.ttf", 14);

draw_point

void draw_point(Point p, RGB color = WHITE);

Draws a point with the specified color.

Example:

win.draw_point(Point(100, 100), BLUE);

draw_line

void draw_line(Point src, Point dst, RGB color = WHITE);

Draws a line from point src to point dst with the specified color.

Example:

win.draw_line(Point(0, 0), Point(100, 100), RED);

draw_rect

void draw_rect(Rectangle rect, RGB color = WHITE, unsigned int line_width = 4);

Draws a rectangle with the specified line_width and color. This will only draw the borders of the rectangle, i.e. the rectangle will be hollow. To draw a filled rectangle, use fill_rect.

Example:

win.draw_rect(Rectangle(100, 100, 50, 50), BLUE);

fill_rect

void fill_rect(Rectangle rect, RGB color = WHITE);

Draws a filled rectangle with the specified color.

Example:

win.fill_rect(Point(100, 100), Point(50, 100), BLUE);

fill_circle

void fill_circle(Point center, int radius, RGB color);

Draws a filled circle with the specified color, center and radius.

Example:

win.fill_circle(Point(100, 100), 10, BLUE);

update_screen

void update_screen();

Updates the window to show the effect of all the methods that have visually manipulated the window. Call this method after all of your calls to draw, fill, or show methods to update the screen.

Example:

win.update_screen();

clear

void clear();

Clears the entire screen.

Example:

win.clear();

has_pending_event

bool has_pending_event();

Checks whether or not there are any unprocessed events waiting in the event queue. Returns true if there are.

bool pending_event_exists = win.has_pending_event();

poll_for_event

Event poll_for_event();

Returns the last event that has occured in window, and removes it from the event queue. If no events have occured since the last call to poll_for_event, an event will be returned whose Event::get_type() method will return NA. But this is not a reliable behavior and it is recommended that you check for the existence of events using Window::has_pending_event().

Example:

Event last_event = win.poll_for_event();

Event

Event objects are returned by window poll_for_event method
You can get event data or event type from event.

Returns type of event.
It can be one the following types:

  • NA: Returned when no event has occured in the window since the last call to poll_for_event.
  • LCLICK: Returned when mouse left button was clicked.
  • RCLICK: Returned when mouse right button was clicked.
  • LRELEASE: Returned when mouse left button is released.
  • RRELEASE: Returned when mouse right button is released.
  • MMOTION: Returned when the mouse cursor moves.
  • KEY_PRESS: Returned when a keyboard key was pressed.
  • KEY_RELEASE: Returned when a keyboard key was released.
  • QUIT: Returned when a quit from program command was issued.

get_type

Event::EventType type = get_type();
switch(type) {
    case Event::LCLICK:
        //handle left-click
        break;
    .
    .
    .
}

you can use the methods introduce so far to perform event handling like this:

while(win.has_pending_event()) {
    Event event = win.poll_for_event();
    switch(event.get_type()) {
        case Event::LCLICK:
            //handle left-click
            break;
        .
        .
        .
    }

}

get_mouse_position

get_mouse_position();

Returns the recent mouse location in window.
Returns relative valuse if event is a mouse motion.
Returns -1 if event is a keyboard key press.

Example:

int mouse_y_location;
if(event.get_type() == Event::LCLICK )
   Point p = event.get_mouse_position();

get_pressed_key

get_pressed_key();

Returns the character value associated with the pressed keyboard key. This method will have undefined behavior if the pressed key does not have a corresponding ASCII character (for example, the arrow keys). Returns -1 if event is not a keyboard key press.

Example:

char pressedChar;
if(event.get_type() == Event::KEY_PRESS ) {
   pressed_char = event.get_pressed_key();
   if (pressed_char == 'w') {
      //handle pressing of the 'w' key 
   }
   .
   .
   .
}

Miscellaneous

delay

delay(int milliseconds);

stops the execution of the programs for a duration of milliseconds milliseconds.

Example:

while (...) {
    win.draw_img("next_frame_of_animation");
    delay(40);  // the animation will have 25fps
}
Clone this wiki locally