Presentation is loading. Please wait.

Presentation is loading. Please wait.

W 2 L 1 sh 1 Assignments Read the ‘overzicht’ document! OnderwerpV2CCPP1 x y x = student nummer y = nummer.

Similar presentations


Presentation on theme: "W 2 L 1 sh 1 Assignments Read the ‘overzicht’ document! OnderwerpV2CCPP1 x y x = student nummer y = nummer."— Presentation transcript:

1 W 2 L 1 sh 1 Assignments Read the ‘overzicht’ document! OnderwerpV2CCPP1 x y x = student nummer y = nummer van de opgave, BijlageDe uitwerking. Als de uitwerking meerdere file beslaat moet de bijlage een.zip file zijn. Geen andere compressie formaten gebruiken! BodyGeen eisen

2 W 2 L 1 sh 2 C lessons LessonSubjectBook Week 1 lesson 1Objects, names Week 1 lesson 2Statements, layout Week 2 lesson 1Functions, decomposition Week 2 lesson 2Lists, specification Week 3 lesson 1Memory, testing Week 3 lesson 2Fagan inspection n The most important way to structure your program n The most important things to name n The most important things to reuse

3 W 2 L 1 sh 3 C function n Interface: n name n argument list n return type n Interface only  declaration n With body  definition (== implementation) n The name identifies the function: two visible functions can not have the same name (!= C++) n The argument list must match the actual arguments passed in each call (in number and type) int give_me_eight( void ){ return 8; } int give_me_eight( void );

4 W 2 L 1 sh 4 n Result n Is what is after the executed ‘return’ statement n Return behaves as an assignment to an invisible variable. n Is the result of the function call Parameters and result n Formal parameters n are mentioned in the interface n Used in the body (implementation) int add( int a, int b ){ return a + b; } int x = add( a, 8 ); n Actual parameters n Is what you (the caller) supply when you can a function n Are (sort-of) assigned to the formal parameters

5 W 2 L 1 sh 5 Parameter passing – by value Basic types are passed ‘by value’ void g( char c, int i, float f, int *p ){ c = 'a'; i = 5; f = 10.0; p = NULL; } int main( void ){ char c = 'x'; int i = 0; float f = 0.0; int *p = &i; printf( "%c %d %f %d \n", c, i, f, (int)p ); g( c, i, f, p ); printf( "%c %d %f %d \n", c, i, f, (int)p ); }

6 W 2 L 1 sh 6 Parameter passing - pointers To change a basic type: pass a pointer to it void g( char *c, int *i, float *f, int **p ){ *c = 'a'; *i = 5; *f = 10.0; *p = NULL; } int main( void ){ char c = 'x'; int i = 0; float f = 0.0; int *p = &i; printf( "%c %d %f %d \n", c, i, f, (int)p ); g( &c, &i, &f, &p ); printf( "%c %d %f %d \n", c, i, f, (int)p ); }

7 W 2 L 1 sh 7 Parameter passing - arrays Arrays are invisibly passed ‘by reference’ == ‘by address’ void f( int a[ 5 ] ){ a[ 0 ] = 5; a[ 1 ] = 4; a[ 2 ] = 3; a[ 3 ] = 2; a[ 4 ] = 1; } int main( void ){ int a[ 5 ] = { 1, 2, 3, 4, 5 }; printf( "%d %d %d %d %d \n", a[0], a[1], a[2], a[3], a[4] ); f( a ); printf( "%d %d %d %d %d \n", a[0], a[1], a[2], a[3], a[4] ); }

8 W 2 L 1 sh 8 Parameter passing – array size Array parameters can either be n Of a specific size n Without a specific size Without a specific size, you will need some other way to find out how large the array is (or you will create the next buffer overrun exploit..) void f( int a[ 5 ] ){ int i; for( i = 0; i < 5; i++ ){... } void f( int a[] ){ int i; for( i = 0; i < ??; i++ ){... }

9 W 2 L 1 sh 9 Function pointers – pass ‘work’ around #include typedef int (*int_filter)( int ); int add1( int x ){ return x + 1; } int clip10( int x ){ return x > 10 ? 10 : x; } int absolute( int x ){ return x > 0 ? x : -x; } void apply_int_filter( int a[], int size, int_filter f ){ int i; for( i = 0; i < size; i++ ){ a[ i ] = f( a[ i ] ); } void array_print( int a[], int size ){ int i; for( i = 0; i < size; i++ ){ printf( "%4d ", a[ i ] ); } printf( "\n" ); }

10 W 2 L 1 sh 10 Function pointers – pass ‘work’ around int main( void ){ #define a_size 7 int a[ a_size ] = { -1, -2, 0, 1, 2, 10, 100 }; array_print( a, a_size ); apply_int_filter( a, a_size, add1 ); array_print( a, a_size ); apply_int_filter( a, a_size, clip10 ); array_print( a, a_size ); apply_int_filter( a, a_size, absolute ); array_print( a, a_size ); system("PAUSE"); return 0; }

11 W 2 L 1 sh 11 A simple NDS paint application #configure board ndsi #configure emulator desmume #include #include "lcd.h" int main( void ){ struct touchPosition position; nintendo_screen lcd; lcd.clear( colors::red ); for(;;){ scanKeys(); int held = keysHeld(); if( held & KEY_TOUCH ){ touchRead( & position ); lcd.pixel_write( position.px, position.py, colors::black ); } swiWaitForVBlank(); } return 0; } mkt.py info libnds graphics lib Lcd lib ‘init’ poll the keys read stylus f = 60 Hz

12 W 2 L 1 sh 12 Paint within an area #define rx0 10 #define ry0 10 #define rx1 200 #define ry1 180 int position_is_in_rectangle( int x, int y, int x0, int y0, int x1, int y1 ){ return( x > x0 && x y0 && y < y1 ); } int main( void ){ struct touchPosition position; nintendo_screen lcd; lcd.clear( colors::red ); lcd.rectangle_write( rx0, ry0, rx1, ry1 ); for(;;){ scanKeys(); int held = keysHeld(); if( held & KEY_TOUCH ){ touchRead( & position ); if( position_is_in_rectangle( position.px, position.py, rx0, ry0, rx1, ry1 )){ lcd.pixel_write( position.px, position.py, colors::blue ); } swiWaitForVBlank(); } return 0; } rectangle check function show paint only when within

13 W 2 L 1 sh 13 But we want more The paint area is just one ‘object’ on the screen. We want more, like changing the brush color. Because (re)drawing and the action are separated we must specify the coordinates twice .  Build a list of ‘active objects’.

14 W 2 L 1 sh 14 Paint - a list of objects typedef struct area_struct area; typedef void ( *action )( int x, int y, area *a ); struct area_struct { area *next; int x0, y0, x1, y1; action f; }; area *first = NULL; void area_add( int x0, int y0, int x1, int y1, color fill, action f ){ area *p = (area*) malloc( sizeof( area )); p->x0 = x0; p->x1 = x1; p->y0 = y0; p->y1 = y1; p->f = f; lcd.rectangle_write( x0, y0, x1, y1, colors::black, fill ); p->next = first; first = p; } ‘forward’ object linked list of objects Create an object and chain it. Q: this is not quality code. Why?

15 W 2 L 1 sh 15 Paint – create the list void execute( int x, int y ){ area *p; for( p = first; p!= NULL; p = p->next ){ if( position_is_in_rectangle( x, y, p->x0, p->y0, p->x1, p->y1 )){ p->f( x, y, p ); } color active_color = colors::blue; void paint( int x, int y, area *a ){ lcd.pixel_write( x, y, active_color ); } void select_red( int x, int y, area *a ){ active_color = colors::red; } void select_green( int x, int y, area *a ){ active_color = colors::green; } void select_blue( int x, int y, area *a ){ active_color = colors::blue; } Check, maybe excute global change-color actions draw-box action

16 W 2 L 1 sh 16 Paint - main int main( void ){ struct touchPosition position; lcd.clear( colors::yellow ); area_add( 10, 10, 200, 180, colors::white, paint ); area_add( 210, 10, 220, 20, colors::red, select_red ); area_add( 210, 25, 220, 35, colors::green, select_green ); area_add( 210, 40, 220, 50, colors::blue, select_blue ); for(;;){ scanKeys(); int held = keysHeld(); if( held & KEY_TOUCH ){ touchRead( & position ); execute( position.px, position.py ); } swiWaitForVBlank(); } return 0; } Create list Execute list

17 W 2 L 1 sh 17 IDE n Make sure you have copied or installed n devkitPro (in C:\devkitPro) n DeSuME (in C:\devkitPro\sim\DeSmuME) n Python (2.5, 26, 3.0, 3.1, etc) n PSPad n get the week-2-1-paint.zip, unzip to a directory, no spaces in the path name !! n Double-click Project.ppr devkitPro.zip

18 W 2 L 1 sh 18 IDE Compile, link, build, run simulator

19 W 2 L 1 sh 19 Assignment n Create an NDS GUI for the reversi code. n Use week-2-1-reversi.zip n Use you own reversi code

20 W 2 L 1 sh 20 Assignment - list struct area_struct { area *next; int field; int x0, y0, x1, y1; action f; }; void area_add( int field, int x0, int y0, int x1, int y1, action f ){ area *p = (area*) malloc( sizeof( area )); p->x0 = x0; p->x1 = x1; p->y0 = y0; p->y1 = y1; p->field = field; p->next = first; p->f = f; first = p; } Field (0..99)

21 W 2 L 1 sh 21 Assignment - main int main( void ){ struct touchPosition position; init_reversi(); redraw(); for(;;){ scanKeys(); int held = keysHeld(); if( held & KEY_TOUCH ){ touchRead( & position ); execute( position.px, position.py ); redraw(); } swiWaitForVBlank(); } return 0; }

22 W 2 L 1 sh 22 Assignment – reversi GUI nintendo_screen lcd; int side_to_move = black; board global_board; int other_side( int x ){ return x == black ? white : black; } color side_color( int side ){ return side == black ? colors::blue : colors :: red; } void try_move( int x, int y, area * a ){ } void redraw_square( area * a ){ } void redraw( void ){ } void init_reversi( void ){ int i, x, y; const int width = 18; reversi_board_init( global_board ); for( i = 0; i < 100; i++ ){ x = 5 + (( i / 10 ) * width ); y = 5 + (( i % 10 ) * width ); area_add( i, x, y, x + width, y + width, try_move ); }

23 W 2 L 1 sh 23 Assignment Function# Lines in my versionRemarks try_move 8If the move is allowed (for the current side-to-move) do it, and update side_to_move. redraw_square 26 (lots of folded lines) Redraw the border around the square. Draw fill for border, empty square. Draw the tile within an occupied square. redraw 12Clear LCD Redraw all squares (call redraw_square to do the work) Redraw ‘side to move’ indicator What if the current side-to-move has no legal moves?


Download ppt "W 2 L 1 sh 1 Assignments Read the ‘overzicht’ document! OnderwerpV2CCPP1 x y x = student nummer y = nummer."

Similar presentations


Ads by Google