diff --git a/common/2d/bitmap.cpp b/common/2d/bitmap.cpp index 81086a83a..f12bd228a 100644 --- a/common/2d/bitmap.cpp +++ b/common/2d/bitmap.cpp @@ -90,13 +90,10 @@ void gr_init_bitmap_data (grs_bitmap *bm) // TODO: virtulize #endif } -grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h ) +grs_subbitmap_ptr gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h ) { - grs_bitmap *n; - - MALLOC(n, grs_bitmap, 1); - gr_init_sub_bitmap (n, bm, x, y, w, h); - + grs_subbitmap_ptr n(new grs_bitmap); + gr_init_sub_bitmap(n.get(), bm, x, y, w, h); return n; } @@ -105,15 +102,6 @@ void gr_free_bitmap(std::unique_ptr bm) gr_free_bitmap_data(bm.get()); } -void gr_free_sub_bitmap(grs_bitmap *bm ) -{ - if (bm!=NULL) - { - d_free(bm); - } -} - - void gr_free_bitmap_data (grs_bitmap *bm) // TODO: virtulize { #ifdef OGL diff --git a/common/include/gr.h b/common/include/gr.h index 5befe494f..d1452fbd3 100644 --- a/common/include/gr.h +++ b/common/include/gr.h @@ -248,16 +248,21 @@ grs_bitmap_ptr gr_create_bitmap(int w,int h); // Allocated a bitmap and makes its data be raw_data that is already somewhere. grs_bitmap_ptr gr_create_bitmap_raw(int w, int h, unsigned char * raw_data ); +// Free the bitmap, but not the pixel data buffer +struct subbitmap_delete : private std::default_delete +{ + using default_delete::operator(); +}; + +typedef std::unique_ptr grs_subbitmap_ptr; + // Creates a bitmap which is part of another bitmap -grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm,int x,int y,int w, int h); +grs_subbitmap_ptr gr_create_sub_bitmap(grs_bitmap *bm,int x,int y,int w, int h); // Free the bitmap's data void gr_free_bitmap_data (grs_bitmap *bm); void gr_init_bitmap_data (grs_bitmap *bm); -// Free the bitmap, but not the pixel data buffer -void gr_free_sub_bitmap(grs_bitmap *bm); - void gr_bm_pixel( grs_bitmap * bm, int x, int y, unsigned char color ); void gr_bm_bitblt(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest); void gr_bm_ubitblt( int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest); diff --git a/similar/main/gauges.cpp b/similar/main/gauges.cpp index 7663ce173..c471d2f75 100644 --- a/similar/main/gauges.cpp +++ b/similar/main/gauges.cpp @@ -347,7 +347,7 @@ bitmap_index Gauges_hires[MAX_GAUGE_BMS]; // hires gauges int weapon_box_user[2]={WBU_WEAPON,WBU_WEAPON}; //see WBU_ constants in gauges.h #endif grs_bitmap deccpt; -grs_bitmap *WinBoxOverlay[2] = { NULL, NULL }; // Overlay subbitmaps for both weapon boxes +static array WinBoxOverlay; // Overlay subbitmaps for both weapon boxes #define PAGE_IN_GAUGE(x) _page_in_gauge(x) static inline void _page_in_gauge(int x) @@ -1790,10 +1790,6 @@ static void cockpit_decode_alpha(grs_bitmap *bm) #ifdef OGL ogl_ubitmapm_cs (0, 0, -1, -1, &deccpt, 255, F1_0); // render one time to init the texture #endif - if (WinBoxOverlay[0] != NULL) - gr_free_sub_bitmap(WinBoxOverlay[0]); - if (WinBoxOverlay[1] != NULL) - gr_free_sub_bitmap(WinBoxOverlay[1]); WinBoxOverlay[0] = gr_create_sub_bitmap(&deccpt,(PRIMARY_W_BOX_LEFT)-2,(PRIMARY_W_BOX_TOP)-2,(PRIMARY_W_BOX_RIGHT-PRIMARY_W_BOX_LEFT+4),(PRIMARY_W_BOX_BOT-PRIMARY_W_BOX_TOP+4)); WinBoxOverlay[1] = gr_create_sub_bitmap(&deccpt,(SECONDARY_W_BOX_LEFT)-2,(SECONDARY_W_BOX_TOP)-2,(SECONDARY_W_BOX_RIGHT-SECONDARY_W_BOX_LEFT)+4,(SECONDARY_W_BOX_BOT-SECONDARY_W_BOX_TOP)+4); @@ -1814,20 +1810,15 @@ static void draw_wbu_overlay() cockpit_decode_alpha(bm); - if (WinBoxOverlay[0] != NULL) - hud_bitblt(HUD_SCALE_X(PRIMARY_W_BOX_LEFT-2),HUD_SCALE_Y(PRIMARY_W_BOX_TOP-2),WinBoxOverlay[0]); - if (WinBoxOverlay[1] != NULL) - hud_bitblt(HUD_SCALE_X(SECONDARY_W_BOX_LEFT-2),HUD_SCALE_Y(SECONDARY_W_BOX_TOP-2),WinBoxOverlay[1]); + if (WinBoxOverlay[0]) + hud_bitblt(HUD_SCALE_X(PRIMARY_W_BOX_LEFT-2),HUD_SCALE_Y(PRIMARY_W_BOX_TOP-2),WinBoxOverlay[0].get()); + if (WinBoxOverlay[1]) + hud_bitblt(HUD_SCALE_X(SECONDARY_W_BOX_LEFT-2),HUD_SCALE_Y(SECONDARY_W_BOX_TOP-2),WinBoxOverlay[1].get()); } void close_gauges() { - if (WinBoxOverlay[0] != NULL) - gr_free_sub_bitmap(WinBoxOverlay[0]); - if (WinBoxOverlay[1] != NULL) - gr_free_sub_bitmap(WinBoxOverlay[1]); - WinBoxOverlay[0] = NULL; - WinBoxOverlay[1] = NULL; + WinBoxOverlay = {}; } void init_gauges() diff --git a/similar/main/newmenu.cpp b/similar/main/newmenu.cpp index ec30ddb09..209927f25 100644 --- a/similar/main/newmenu.cpp +++ b/similar/main/newmenu.cpp @@ -103,16 +103,12 @@ struct newmenu }; grs_bitmap nm_background, nm_background1; -grs_bitmap *nm_background_sub = NULL; +static grs_subbitmap_ptr nm_background_sub; void newmenu_free_background() { if (nm_background.bm_data) { - if (nm_background_sub) - { - gr_free_sub_bitmap(nm_background_sub); - nm_background_sub = NULL; - } + nm_background_sub.reset(); gr_free_bitmap_data (&nm_background); } if (nm_background1.bm_data) @@ -186,12 +182,10 @@ void nm_draw_background(int x1, int y1, int x2, int y2 ) if (!init_sub && ((nm_background_sub->bm_w != w*(((float) nm_background.bm_w)/SWIDTH)) || (nm_background_sub->bm_h != h*(((float) nm_background.bm_h)/SHEIGHT)))) { init_sub=1; - gr_free_sub_bitmap(nm_background_sub); - nm_background_sub = NULL; } if (init_sub) nm_background_sub = gr_create_sub_bitmap(&nm_background,0,0,w*(((float) nm_background.bm_w)/SWIDTH),h*(((float) nm_background.bm_h)/SHEIGHT)); - show_fullscr( nm_background_sub ); + show_fullscr( nm_background_sub.get() ); gr_set_current_canvas(old); gr_free_sub_canvas(tmp);