classes.pdf

(141 KB) Pobierz
abstract class Root
// type primary constructor: Root_init_type
Root *Root_init_type(
void *child,
roottype type,
void
*object,
perr
(*object_vdestructor)(void*)) {
/* Check parameters */
if ((! child) || (! object) || (! object_vdestructor)) return NULL;
/* Allocate new root object */
Root *root = malloc(sizeof(Root));
if (! root) return NULL;
/* Initialize fields */
root->this = root;
root->object = object;
root->object_vdestructor = object_vdestructor;
root->child = child;
root->type = type;
/* Return newly created object */
return root;
}
// type copy constructor: Root_copy
Root *Root_copy(Root *root_src, void *child_dest, void *object_dest) {
if ((! root_src) || (! child_dest) || (! object_dest)) return NULL;
/* Allocate new root object */
Root *root_dest = malloc(sizeof(Root));
if (! root_dest) return NULL;
/* Initialize fields */
root_dest->this = root_dest;
root_dest->object = object_dest;
if (! root_src->object_vdestructor) { WARNING destructor is NULL is source; }
root_dest->object_vdestructor
= root_src->object_vdestructor;
root_dest->child
= child_dest;
root_dest->type
= root_src->type;
/* Return newly created object */
return root_dest;
}
// First destructor: Root_destroy (destroys only root object instance)
perr Root_destroy(Root *root) {
if (! root) throw E_INTRO_FAILED;
/* Delete self */
free(root);
throw E_NO_ERROR;
}
// Second destructor: Root_destroyAll (destroys also object)
perr Root_destroyAll(Root *root) {
if (! root) throw E_INTRO_FAILED;
/* Delete object */
if ((root->object_vdestructor) && (root->object))
root->object_vdestructor(root->object);
else WARNING Found NULL destructor pointer or NULL object;
/* Delete self */
free(root);
throw E_NO_ERROR;
}
abstract class Widget
// type primary constructor: Widget_init_type
Widget *Widget_init_type( void *child,
widgettype type,
void
*object,
perr
(*object_vdestructor)(void*)) {
/* Check parameters */
if ((! child) || (! object) || (! object_vdestructor)) return NULL;
/* Allocate new widget object */
Widget *widget = malloc(sizeof(Widget));
if (! widget) return NULL;
/* Allocate widget position */
if (! (widget->pos = malloc(sizeof(SDL_Rect)))) { free(widget); return NULL; }
/* Create parent root object */
widget->root = Root_init_type(widget, Root_Widget,
object, object_vdestructor);
if (! widget->root) { free(widget->pos); free(widget); return NULL; }
/* Initialize widget fields */
widget->this
= widget;
widget->type
= type;
widget->child
= child
...
/* Return newly created object */
return widget;
}
// type copy constructor: Widget_copy
Widget *Widget_copy( Widget *widget_src,
void *child_dest,
void *object_dest) {
if ((! widget_src) || (! child_dest) || (! object_dest)) return NULL;
/* Allocate new widget object */
Widget *widget_dest = malloc(sizeof(Widget));
if (! widget_dest) return NULL;
/* Allocate widget position */
if (! (widget_dest->pos = malloc(sizeof(SDL_Rect))))
{ free(widget_dest); return NULL; }
/* Create parent root object */
widget_dest->root = Root_copy(widget_src->root, widget_dest, object_dest);
if (! widget_dest->root)
{ free(widget_dest->pos); free(widget_dest); return NULL; }
/* Initialize widget fields */
widget_dest->this
= widget_dest;
widget_dest->child
= child_dest;
widget_dest->type
= widget_dest->type;
...
/* Return newly created object */
return widget_dest;
}
// Destructor: Widget_destroy (called manually)
perr Widget_destroy(Widget *widget) {
if (! widget) throw E_INTRO_FAILED;
perr e, e2 = E_NO_ERROR;
/* Delete parent root object */
if (e = Root_destroy(widget->root)) { WARNING cannot destroy parent root; e2 = e; }
/* Delete widget surface */
if (widget->surf) SDL_FreeSurface(widget->surf);
/* Delete vparam array */
if (widget->vparam) free(widget->vparam);
/* Delete self */
free(widget);
throw e2;
}
class Screen
// First constructor: Screen_new_primary (also primary screen initialize)
Screen *Screen_new_primary(uint approx_widget_count) {
/* Check whether primary screen isn't already initialized */
if (is_primary_inited) { PRIMARY IS ONLY ONE; return NULL; }
/* Allocate new screen object */
Screen *screen = malloc(sizeof(Screen));
if (! screen) return NULL;
/* Allocate SDL_Event object */
screen->pevent = malloc(sizeof(SDL_Event));
if (! screen->pevent) { free(screen); return NULL; }
/* Create parent root object */
screen->root = Root_init_type(screen, Root_Screen, screen, Screen_vdestroy);
if (! screen->root) { free(screen->pevent); free(screen); return NULL; }
/* Create screen widget Array */
screen->widget_array = Array_new(approx_widget_count);
if (! screen->widget_array)
{ Root_destroy(screen->root); free(screen->pevent);
free(screen); return NULL; }
/* Initialize SDL libraries */
perr e = Screen_MainInit(screen);
if (e) { ERROR perr_getName(e); Array_destroy(screen->widget_array);
Root_destroy(screen->root); free(screen->pevent); free(screen);
return NULL; }
/* Initialize fields */
screen->this = screen;
screen->callback = NULL; /* use Screen_CallbackCreate to create and use cparams */
screen->is_primary= true ;
screen->background= NULL; /* use Screen_setBackgroundImage to set background img */
...
is_primary_inited = true;
return screen;
}
// Second constructor: Screen_new_secondary (need pointer to primary Screen
instance)
Screen *Screen_new_secondary( Screen *screen_primary,
uint approx_widget_count) {
/* validate screen_primary parameter and check if primary is initialized */
if ((! screen_primary) || (! is_primary_inited) || (! screen_primary->screen))
return NULL;
/*! Below almost copy of Screen_new_primary !*/
/* Allocate new screen object */
Screen *screen = malloc(sizeof(Screen));
if (! screen) return NULL;
/* Allocate SDL_Event object */
screen->pevent = malloc(sizeof(SDL_Event));
if (! screen->pevent) { free(screen); return NULL; }
/* Create parent root object */
screen->root = Root_init_type(screen, Root_Screen, screen, Screen_vdestroy);
if (! screen->root) { free(screen->pevent); free(screen); return NULL; }
/* Create screen widget Array */
screen->widget_array = Array_new(approx_widget_count);
if (! screen->widget_array)
{ Root_destroy(screen->root); free(screen->pevent);
free(screen); return NULL; }
/* Initialize fields */
screen->this = screen;
screen->callback = NULL; /* use Screen_CallbackCreate to create and use cparams */
screen->is_primary= false ;
screen->background= NULL; /* use Screen_setBackgroundImage to set background img */
...
return screen;
}
// Destructor: Screen_destroy (called manually)
perr Screen_destroy(Screen *screen) {
if (! screen) throw E_INTRO_FAILED;
perr e, e2 = E_NO_ERROR;
/* Deinitialize SDL libraries if primary */
if ((is_primary_inited) && (screen->is_primary) &&
(e = Screen_MainDestroy(screen))) { if ( { ERROR perr_getName(e); e2 = e; }
/* Destroy parent root object */
if ((screen->root) && (e = Root_destroy(screen->root)))
{ ERROR perr_getName(e); e2 = e; }
/* Destroy widgets Array */
if ((screen->array) && (e = Array_destroy(screen->array)))
{ ERROR perr_getName(e); e2 = e; }
/* Destroy Callback */
if ((screen->callback) && (e = Callback_destroy(screen->callback)))
{ ERROR perr_getName(e); e2 = e; }
/* Destroy background image */
if ((screen->background) && (e = Image_destroy(screen->background)))
{ ERROR perr_getName(e); e2 = e; }
/* Deallocate SDL_Event object */
if (screen->pevent) free(screen->pevent);
/* Deallocate self */
free(screen);
throw e2;
}
// Destructor: Screen_vdestroy (called by Root)
perr Screen_vdestroy(void *screen) {
if (! screen) throw E_INTRO_FAILED;
perr e = Screen_destroy((Screen*)screen);
throw e;
}
class Image
// Primary constructor: Image_new
Image *Image_new(char *path, usint minx, usint miny) {
if (! path) { ERROR null path; return NULL; }
/* Check whether image file exists */
if (! Static_fileExist(path)) { ERROR bad path; return NULL; }
/* Allocate new image object */
Image *image = malloc(sizeof(Image));
if (! image) { ERROR bad alloc; return NULL; }
/* Initialize parent Widget */
image->widget = Widget_init_type(image, Widget_Image, image, Image_vdestroy);
if (! image->widget) { ERROR parent init failed; free(image); return NULL; }
/* Load image (png, tiff, gif, jpeg) */
if (! (image->widget->surf = IMG_Load(path)))
{ ERROR IMG_Load; Widget_destroy(image->widget); free(image); return NULL; }
/* Set position of Image */
Widget_setPosition(image->widget, minx, miny);
/* Copy path */
image->path = Static_copyText(path);
if (! image->path) { WARNING cannot copy path; }
/* Initialize this pointer */
image->this = image;
/* Set image visible and doesn't need refresh */
image->widget->visible = true;
image->widget->need_refresh = false;
/* Return newly created image */
return image;
}
// Copy constructor: Image_copy
Image *Image_copy(Image *img_src) {
if (! img_src) { ERROR null source image; return NULL; }
/* Allocate new image object */
Image *img_dest = malloc(sizeof(Image));
if (! img_dest) { ERROR bad alloc; return NULL; }
/* Initialize parent Widget */
img_dest->widget = Widget_copy(img_src->widget, img_dest, img_dest);
if (! img_dest->widget) { ERROR parent init failed; free(img_dest); return NULL; }
/* Copy path */
img_dest->path = Static_copyText(img_src->path);
if (! img_dest->path) { WARNING cannot copy path; }
/* Initialize this pointer */
img_dest->this = img_dest;
/* Set image visible and doesn't need refresh */
img_dest->widget->visible = true;
img_dest->widget->need_refresh = false;
/* Return newly created image */
return img_dest;
}
// Destructor: Image_destroy (called manually)
perr Image_destroy(Image *image) {
if (! image) { ERROR null parameter; throw E_INTRO_FAILED; }
/* Destroy parent widget */
perr e = Widget_destroy(image->widget);
if (e) { ERROR failed to destroy parent widget; }
/* Delete path */
if (image->path) free(image->path);
/* Delete self */
free(image);
Zgłoś jeśli naruszono regulamin