Title: Solution: Callbacks
1Solution Callbacks
EasyGL checks the event queue and calls the
appropriate callback ? now myProg.exe can handle
it.
myProg.exe
This course EasyGL (simple, cross-platform
graphics)
myProg.exe registers callback functions for
various events
Hardware receives the input and X11 inserts event
into event queue
x11 API
Then hands control to EasyGL
User resizes window or clicks on a button
2Handing Control to EasyGL
Optional callbacks not using here
Callback function to redraw the screen.
- int main ()
- ...
- event_loop (NULL, NULL, NULL, draw_screen)
- ...
- void draw_screen (void)
- // Your screen redraw function.
- clearscreen () // Erase old graphics.
- // Should always be first
line - setcolor (RED)
- fillpoly (...)
- my_drawing_func1 ()
- ...
Any name you want, but must have the right
function prototype
3event_loop ()
Your drawing calls (drawline etc.) render here
(graphics area)
EasyGL made and manages these buttons
Zooming Panning EasyGL adjust how your drawing
appears on screen to zoom pan
4event_loop ()
calling event_loop() ? hands control to EasyGL
event_loop () does not return until you hit the
Proceed button
Use update_message (string) to put text here
5Other Callbacks Mouse Button Presses
Called when a mouse button pressed in the
graphics area
- int main ()
- ...
- event_loop (act_on_mouse_button, NULL, NULL,
- draw_screen)
- ...
- void act_on_mouse_button (float x, float y,
- t_event_buttonPressed buttonPressed)
- cout ltlt Mouse button press at ( ltlt x ltlt ,
- ltlt y ltlt
)\n - if buttonPressed.ctrl_pressed
- cout ltlt Ctrl was held down too!\n
mouse button callback must have this function
signature
6More Optional Callbacks
Optional callbacks for when a keyboard key is hit
(keypress) and when the mouse is moved in the
graphics area
- int main ()
- ...
- event_loop (act_on_mouse_button,
act_on_mousemove, act_on_keypress,
draw_screen) - ...
- void act_on_mousemove (float x, float y)
- // x and y are the coordinates of the cursor
in - // world coordinates
- . . . // Do something ...
-
- void act_on_keypress (char key_pressed)
- // This key was just pressed. Do something!
- . . .
-
7Notes
- No need to call flushinput () inside your redraw
function ? the event loop itself will make
redraws appear - You can make your own buttons with create_button
() - Give each a callback function for when it is
pressed - ECE 244
- Used a simplified wrapper to EasyGL
- This course all routines available
8Cleaning Up
- Last call close down window
- close_graphics ()
9World Coordinate System You Choose!
E.g., use km from Prime Meridian Equator
set_visible_world (-2000, 4010, -1950, 4025)
world (-1950,4034.7)
world (-2000,4000.3)
Maintains aspect ratio
10Screen (Pixel) Coordinates Fixed
world (-1950,4034.7)
screen (0, 0)
world (-2000,4000.3)
screen (900, 619)
11Panning Zooming EasyGL Transforms
world (-1960,4026.5)
easygl changes the mapping from world ? screen
pixels Calls your drawscreen () Only a part of
your picture shows up on screen
world (-1990,4007.8)
12Pan Zoom (Transform) Implications
- Your drawscreen() callback can always draw the
whole map (world) - Only proper part will show up on screen
- Costs some CPU time to draw objects that arent
on screen, but less than you might expect
(pre-clipped) - Dont call set_visible_world() in drawscreen()
- Would disable panning and zooming
13Level of Detail
- Zoomed way in ? can show small details (street
names, small streets, ) - Zoomed way out ? draw every detail
14Level of Detail Utilities
- setfont (10) // 10 point font ? 10/72 inch high
- t_point center (-1970, 4020)
- float streetLength 0.2 // 200 m long.
- drawtext (center, Awesome street, streetLen,
streetLen)
Fonts are in screen coordinates, not world
coordinates. What if this text is much longer
than the street?
Awesome Street
Electric Avenue
My Street
AYour Street
Sesame Street
E. Street
Easy Street
Text wont be drawn if it is wider or higher than
these numbers, in world coordinates
Use a giant length (e.g. FLT_MAX) if you want the
text to be drawn no matter what
15Level of Detail Utilities
- t_bound_box get_visible_world()
- Returns a rectangle ? the world coordinates of
the screen edges - Use to decide if youre zoomed in or not
- bool LOD_screen_area_test(t_bound_box test,
- float screen_area_threshold)
- Returns true if the (world coordinate) test
rectangle will fill more than screen_area_threshol
d pixels