/* handle an overlapping band for intersect_region */ static int intersect_overlapping( struct region *pReg, const rectangle_t *r1, const rectangle_t *r1End, const rectangle_t *r2, const rectangle_t *r2End, int top, int bottom ) { int left, right; while ((r1 != r1End) && (r2 != r2End)) { left = max(r1->left, r2->left); right = min(r1->right, r2->right); if (left < right) { rectangle_t *rect = add_rect( pReg ); if (!rect) return 0; rect->left = left; rect->top = top; rect->right = right; rect->bottom = bottom; } if (r1->right < r2->right) r1++; else if (r2->right < r1->right) r2++; else { r1++; r2++; } } return 1; }
void split_rects(struct nhrect *r1, struct nhrect *r2) { struct nhrect r, old_r; int i; old_r = *r1; remove_rect(r1); /* Walk down since rect_cnt & rect[] will change... */ for (i = rect_cnt - 1; i >= 0; i--) if (intersect(&rect[i], r2, &r)) split_rects(&rect[i], &r); if (r2->ly - old_r.ly - 1 > (old_r.hy < ROWNO - 1 ? 2 * YLIM : YLIM + 1) + 4) { r = old_r; r.hy = r2->ly - 2; add_rect(&r); } if (r2->lx - old_r.lx - 1 > (old_r.hx < COLNO - 1 ? 2 * XLIM : XLIM + 1) + 4) { r = old_r; r.hx = r2->lx - 2; add_rect(&r); } if (old_r.hy - r2->hy - 1 > (old_r.ly > 0 ? 2 * YLIM : YLIM + 1) + 4) { r = old_r; r.ly = r2->hy + 2; add_rect(&r); } if (old_r.hx - r2->hx - 1 > (old_r.lx > 0 ? 2 * XLIM : XLIM + 1) + 4) { r = old_r; r.lx = r2->hx + 2; add_rect(&r); } }
/* handle a non-overlapping band for union_region */ static int union_non_overlapping( struct region *pReg, const rectangle_t *r, const rectangle_t *rEnd, int top, int bottom ) { while (r != rEnd) { rectangle_t *rect = add_rect( pReg ); if (!rect) return 0; rect->left = r->left; rect->top = top; rect->right = r->right; rect->bottom = bottom; r++; } return 1; }
static void test_bounds(skiatest::Reporter* reporter) { static const SkRect rects[] = { { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) }, { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) }, { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) }, { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) }, }; SkPath path0, path1; for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) { path0.addRect(rects[i]); add_rect(&path1, rects[i]); } REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds()); }
static float add_glyph(int gid, float x, float y) { struct glyph *glyph; int subx = (x - floor(x)) * XPRECISION; int suby = (y - floor(y)) * YPRECISION; glyph = lookup_glyph(text_font, text_scale, gid, subx, suby); if (!glyph) return 0; float s0 = (float) glyph->s / CACHESIZE; float t0 = (float) glyph->t / CACHESIZE; float s1 = (float) (glyph->s + glyph->w) / CACHESIZE; float t1 = (float) (glyph->t + glyph->h) / CACHESIZE; float xc = floor(x) + glyph->x; float yc = floor(y) + glyph->y; add_rect(xc, yc, xc + glyph->w, yc + glyph->h, s0, t0, s1, t1); return glyph->advance; }
int game_loop (int *ret_bounces, int *ret_role, int *ret_most, int *ret_tickets) { int done = 0; SDL_Event event; SDLKey key; Uint32 last_time, now_time; SDL_Rect puf_pos, rect; int sonido; int last_button = 0, button_frame, button_pressed; int handposx2, handposx1, handposx, handposy2, handposy1, handposy; /* Para calcular los desplazamientos del mouse */ int fuerzax, fuerzay; /* Calculos de fuerza al golpear el puffle */ float poder; float speed = 10, balance = 4; int wind = 1, wind_countdown = 240; /* Para evitar puffles estancados verticalmente */ int n_puffles = 1, most_puffles = 1, dropped_puffles = 0; /* Llevar la cantidad de puffles */ int count = 0, goal = 20, default_goal = 20; /* Para control de la generación de próximos puffles */ int bounces = 0, role = 0; /* Bounces, golpes totales. Role, el mayor número de golpes */ int paddle_frame = 0; int tickets = 0; Puffle *thispuffle; SDL_Surface *text_num; char text_buffer[6]; int pop_timer = -1, pop_x, pop_y, pop_num; nuevo_puffle (); background_frame = 0; SDL_EventState (SDL_MOUSEMOTION, SDL_IGNORE); SDL_GetMouseState (&handposx, &handposy); handposx2 = handposx1 = handposx; handposy2 = handposy1 = handposy; do { last_time = SDL_GetTicks (); while (SDL_PollEvent(&event) > 0) { switch (event.type) { case SDL_QUIT: /* Vamos a cerrar la aplicación */ done = GAME_QUIT; break; case SDL_MOUSEBUTTONDOWN: /* Tengo un Mouse Down */ if (event.button.button != SDL_BUTTON_LEFT) break; if (last_button == BUTTON_NONE) last_button = map_button_in_game (event.button.x, event.button.y); if (use_sound && last_button == BUTTON_CLOSE) Mix_PlayChannel (-1, sounds[SND_OVER_TO_DOWN1], 0); break; case SDL_MOUSEBUTTONUP: /* Tengo un mouse Up */ if (event.button.button != SDL_BUTTON_LEFT) break; if (last_button != BUTTON_NONE) { if (map_button_in_game (event.button.x, event.button.y) == last_button) { /* Mouse down y mouse up sobre el mismo botón */ /* Utilizar switch para muchos botones */ if (last_button == BUTTON_CLOSE) { done = GAME_QUIT; } button_pressed = last_button; } last_button = BUTTON_NONE; } break; case SDL_KEYDOWN: /* Tengo una tecla presionada */ key = event.key.keysym.sym; /*if (key == SDLK_z) { fprintf (stderr, "Sending new background\n"); background_frame = background_frames [background_frame][BACKGROUND_NEW]; whole_flip = 1; } else if (key == SDLK_x) { fprintf (stderr, "Sending fail background\n"); background_frame = background_frames [background_frame][BACKGROUND_FAIL]; whole_flip = 1; }*/ /*if (key == SDLK_q) { fprintf (stderr, "Bounces = 60, activando wind\n"); bounces = 60; } else if (key == SDLK_w) { fprintf (stderr, "Bounces = 0, desactivando wind\n"); bounces = 0; }*/ if (key == SDLK_F11 || (key == SDLK_RETURN && (event.key.keysym.mod & KMOD_ALT))) { SDL_WM_ToggleFullScreen (screen); } if (key == SDLK_ESCAPE) { done = GAME_QUIT; } break; } } if (count >= goal) { count = 0; n_puffles++; if (n_puffles > 4) { goal = default_goal; if (default_goal > 5) { default_goal--; } if (dropped_puffles > 49) { default_goal += 20; dropped_puffles = 0; } } else if (n_puffles >= most_puffles) { goal = n_puffles * 20; } else { goal = n_puffles * 10; } if (most_puffles < n_puffles) { most_puffles = n_puffles; } nuevo_puffle (); } handposy2 = handposy1; handposy1 = handposy; handposx2 = handposx1; handposx1 = handposx; SDL_GetMouseState (&handposx, &handposy); fuerzay = handposy2 - handposy; if (fuerzay > 0) poder = fuerzay / 6; else poder = 0; fuerzax = handposx2 - handposx; thispuffle = first_puffle; do { if (thispuffle->y > 530) { /* Este puffle está perdido */ n_puffles--; dropped_puffles++; if (n_puffles > 4) { goal = default_goal; } else if (n_puffles >= most_puffles) { goal = n_puffles * 20; } else { goal = n_puffles * 10; } if (thispuffle->prev != NULL) { thispuffle = thispuffle->prev; eliminar_puffle (thispuffle->next); } else { eliminar_puffle (thispuffle); thispuffle = first_puffle; } } if (thispuffle != NULL) thispuffle = thispuffle->next; } while (thispuffle != NULL); if (first_puffle == NULL) { done = GAME_CONTINUE; tickets = bounces + most_puffles * role; *ret_tickets = tickets; *ret_bounces = bounces; *ret_most = most_puffles; *ret_role = role; break; } if (background_frame != 0) whole_flip = 1; /* Avanzar el escenario */ background_frame = background_frames [background_frame][BACKGROUND_NORMAL]; if (whole_flip) { SDL_BlitSurface (images [background_outputs[background_frame]], NULL, screen, NULL); } thispuffle = first_puffle; do { /* Borrar los puffles */ puf_pos.x = ((int) thispuffle->x) - 48; /* Constante temporal */ puf_pos.y = ((int) thispuffle->y) - 60; /* Constante temporal */ puf_pos.w = images [IMG_BLUE_NORMAL_1]->w; puf_pos.h = images [IMG_BLUE_NORMAL_1]->h; SDL_BlitSurface (images [background_outputs[background_frame]], &puf_pos, screen, &puf_pos); add_rect (&puf_pos); /* Avanzar de posición los puffles */ thispuffle->x = thispuffle->x + thispuffle->x_virtual; thispuffle->y = thispuffle->y + thispuffle->y_virtual; if (thispuffle->x >= 720 && thispuffle->x_virtual >= 0) thispuffle->x_virtual *= -1; else if (thispuffle->x <= 40 && thispuffle->x_virtual < 0) thispuffle->x_virtual *= -1; if (thispuffle->y_virtual < -10) thispuffle->y_virtual *= 0.9; else thispuffle->y_virtual += 1.0; /* 1 de "Gravity" */ if (thispuffle->x_virtual > 30 || thispuffle->x_virtual < -30) { thispuffle->x_virtual *= 0.95; } /* Si tiene un score (alias bounces) mayor a 50, aplicar un poco de viento */ if (bounces > 50) { /* Bounces ajustado a 25, valor original 50 */ if (wind) thispuffle->x_virtual += 0.1; else thispuffle->x_virtual -= 0.1; if (wind_countdown > 0) wind_countdown--; } if (wind_countdown == 0) { if (wind) wind = 0; else wind = 1; wind_countdown = 240; } if (pop_timer != -1) { /* Borrar el pop */ puf_pos.x = pop_x - 57; puf_pos.y = pop_y + 7; puf_pos.w = 114; puf_pos.h = 86; SDL_BlitSurface (images [background_outputs[background_frame]], &puf_pos, screen, &puf_pos); add_rect (&puf_pos); if (pop_timer > 9) { pop_timer = -1; } } if (thispuffle->y > -99 && thispuffle->y_virtual >= 0) { if ((((int) thispuffle->x) > handposx - 70 && ((int) thispuffle->x) < handposx + 70) && ((((int) thispuffle->y) + 30 > handposy && ((int) thispuffle->y) + 30 < handposy + 100) || (((int) thispuffle->y) > handposy && ((int) thispuffle->y) < handposy2))) { /* Bounce the puffle */ sonido = SND_SQUEAK1 + (int) (2.0 * rand () / (RAND_MAX + 1.0)); if (fuerzax > 300 || fuerzax < -300 || poder > 30) { sonido = SND_SQUEAK3; } thispuffle->x_virtual = (thispuffle->x - (handposx + fuerzax)) / balance; thispuffle->y_virtual = -1 * (speed + poder); bounces++; count++; if (speed < 40) speed += 0.2; else if (balance > 2) balance -= 0.2; pop_num = ++thispuffle->pop_num; pop_timer = 0; pop_x = ((int) thispuffle->x); pop_y = ((int) thispuffle->y); if (thispuffle->pop_num > role) role = thispuffle->pop_num; thispuffle->frame = puffle_frames [thispuffle->frame][PUFFLE_BOUNCE]; paddle_frame = paddle_frames [paddle_frame][PADDLE_BOUNCE]; if (use_sound) Mix_PlayChannel (-1, sounds[sonido], 0); tickets = bounces + most_puffles * role; } else if ((((int) thispuffle->y) + 30 > handposy && ((int) thispuffle->y) + 30 < handposy + 100) && ((((int) thispuffle->x) > handposx && ((int) thispuffle->x) < handposx2) || (((int) thispuffle->x) < handposx && ((int) thispuffle->x) > handposx2))) { /* Bounce the puffle */ sonido = SND_SQUEAK1 + (int) (2.0 * rand () / (RAND_MAX + 1.0)); if (fuerzax > 300 || fuerzax < -300 || poder > 30) { sonido = SND_SQUEAK3; } thispuffle->x_virtual = (thispuffle->x - (handposx + fuerzax)) / balance; thispuffle->y_virtual = -1 * (speed + poder); bounces++; count++; if (speed < 40) speed += 0.2; else if (balance > 2) balance -= 0.2; pop_num = ++thispuffle->pop_num; pop_timer = 0; pop_x = thispuffle->x; pop_y = thispuffle->y; if (thispuffle->pop_num > role) role = thispuffle->pop_num; thispuffle->frame = puffle_frames [thispuffle->frame][PUFFLE_BOUNCE]; paddle_frame = paddle_frames [paddle_frame][PADDLE_BOUNCE]; if (use_sound) Mix_PlayChannel (-1, sounds[sonido], 0); tickets = bounces + most_puffles * role; } } if (thispuffle->y_virtual > 10) { thispuffle->frame = puffle_frames [thispuffle->frame][PUFFLE_FALL]; } thispuffle->frame = puffle_frames [thispuffle->frame][PUFFLE_NORMAL]; if (thispuffle != NULL) thispuffle = thispuffle->next; } while (thispuffle != NULL); /* Pre-borrar la cantidad de tickets */ puf_pos.x = 607; puf_pos.y = 59; puf_pos.w = 136; puf_pos.h = 26; SDL_BlitSurface (images [background_outputs[background_frame]], &puf_pos, screen, &puf_pos); add_rect (&puf_pos); /* Preborrar la paleta */ puf_pos.x = handposx1 - 58; puf_pos.y = handposy1 - 70; puf_pos.h = images [IMG_PADDLE_1]->h; puf_pos.w = images [IMG_PADDLE_1]->w; SDL_BlitSurface (images [background_outputs[background_frame]], &puf_pos, screen, &puf_pos); add_rect (&puf_pos); /* Pre-borrar el botón de cierre */ puf_pos.x = 720; puf_pos.y = 8; puf_pos.h = images[IMG_CLOSE_BUTTON_UP]->h; puf_pos.w = images[IMG_CLOSE_BUTTON_UP]->w; SDL_BlitSurface (images [background_outputs[background_frame]], &puf_pos, screen, &puf_pos); add_rect (&puf_pos); /* Dibujar la cantidad de tickets */ sprintf (text_buffer, "%d", tickets); text_num = draw_text_with_shadow (ttf16_normal, ttf16_outline, text_buffer); puf_pos.x = 607 + ((136 - text_num->w) / 2); puf_pos.y = 60 + ((26 - text_num->h) / 2); puf_pos.w = text_num->w; puf_pos.h = text_num->h; SDL_BlitSurface (text_num, NULL, screen, &puf_pos); add_rect (&puf_pos); SDL_FreeSurface (text_num); paddle_frame = paddle_frames[paddle_frame][PADDLE_NORMAL]; /* Blit el paddle */ puf_pos.x = handposx - 58; /* Constante temporal */ puf_pos.y = handposy - 70; /* Constante temporal */ puf_pos.h = images [IMG_PADDLE_1]->h; puf_pos.w = images [IMG_PADDLE_1]->w; SDL_BlitSurface (images [paddle_outputs [paddle_frame]], NULL, screen, &puf_pos); add_rect (&puf_pos); /* Dibujar el botón de cierre */ /* Posición original X:734, Y:22 * Centro +- 14.05 */ puf_pos.x = 720; puf_pos.y = 8; if (button_pressed == BUTTON_CLOSE || (last_button == BUTTON_CLOSE && map_button_in_game (handposx, handposy) == BUTTON_CLOSE)) { /* Está presionado el botón del mouse, y está sobre el botón */ button_frame = IMG_CLOSE_BUTTON_DOWN; button_pressed = BUTTON_NONE; } else if (last_button == BUTTON_CLOSE) { button_frame = IMG_CLOSE_BUTTON_OVER; } else if (last_button == BUTTON_NONE && map_button_in_game (handposx, handposy) == BUTTON_CLOSE) { button_frame = IMG_CLOSE_BUTTON_OVER; } else { button_frame = IMG_CLOSE_BUTTON_UP; } SDL_BlitSurface (images[button_frame], NULL, screen, &puf_pos); add_rect (&puf_pos); thispuffle = first_puffle; do { if (thispuffle->y > -100) { /* Blit this puffle */ puf_pos.x = ((int) thispuffle->x) - 48; /* Constante temporal */ puf_pos.y = ((int) thispuffle->y) - 60; /* Constante temporal */ puf_pos.w = images [puffle_outputs [thispuffle->frame]]->w; puf_pos.h = images [puffle_outputs [thispuffle->frame]]->h; SDL_BlitSurface (images [puffle_outputs [thispuffle->frame] + (thispuffle->color * 8)], NULL, screen, &puf_pos); add_rect (&puf_pos); } if (thispuffle != NULL) thispuffle = thispuffle->next; } while (thispuffle != NULL); /* Dibujar el pop */ if (pop_timer >= 0) { if (pop_timer < 6) { puf_pos.x = pop_x - 57; puf_pos.y = pop_y + 7; rect.w = puf_pos.w = 114; rect.h = puf_pos.h = 86; rect.y = 0; rect.x = pop_timer * rect.w; SDL_BlitSurface (images[IMG_POP], &rect, screen, &puf_pos); add_rect (&puf_pos); sprintf (text_buffer, "%d", pop_num); text_num = draw_text_with_shadow (ttf16_normal, ttf16_outline, text_buffer); puf_pos.x = pop_x - (text_num->w / 2); puf_pos.y = pop_y + 40; puf_pos.w = text_num->w; puf_pos.h = text_num->h; SDL_BlitSurface (text_num, NULL, screen, &puf_pos); SDL_FreeSurface (text_num); } else { sprintf (text_buffer, "%d", pop_num); text_num = draw_text_with_shadow (ttf16_normal, ttf16_outline, text_buffer); puf_pos.x = pop_x - (text_num->w / 2); puf_pos.y = pop_y + 40; puf_pos.w = text_num->w; puf_pos.h = text_num->h; SDL_BlitSurface (text_num, NULL, screen, &puf_pos); SDL_FreeSurface (text_num); add_rect (&puf_pos); } pop_timer++; } if (whole_flip) { whole_flip = 0; SDL_Flip (screen); } else { SDL_UpdateRects (screen, num_rects, rects); num_rects = 0; } now_time = SDL_GetTicks (); if (now_time < last_time + FPS) SDL_Delay(last_time + FPS - now_time); } while (!done); return done; }
/* handle an overlapping band for subtract_region */ static int subtract_overlapping( struct region *pReg, const rectangle_t *r1, const rectangle_t *r1End, const rectangle_t *r2, const rectangle_t *r2End, int top, int bottom ) { int left = r1->left; while ((r1 != r1End) && (r2 != r2End)) { if (r2->right <= left) r2++; else if (r2->left <= left) { left = r2->right; if (left >= r1->right) { r1++; if (r1 != r1End) left = r1->left; } else r2++; } else if (r2->left < r1->right) { rectangle_t *rect = add_rect( pReg ); if (!rect) return 0; rect->left = left; rect->top = top; rect->right = r2->left; rect->bottom = bottom; left = r2->right; if (left >= r1->right) { r1++; if (r1 != r1End) left = r1->left; } else r2++; } else { if (r1->right > left) { rectangle_t *rect = add_rect( pReg ); if (!rect) return 0; rect->left = left; rect->top = top; rect->right = r1->right; rect->bottom = bottom; } r1++; left = r1->left; } } while (r1 != r1End) { rectangle_t *rect = add_rect( pReg ); if (!rect) return 0; rect->left = left; rect->top = top; rect->right = r1->right; rect->bottom = bottom; r1++; if (r1 != r1End) left = r1->left; } return 1; }
void Shape2D::add_rect(const Rectf &rect, bool reverse) { add_rect(rect, Angle(), reverse); }
void t_sorted_clip_list::remove( t_screen_rect const & remove_rect_area ) { //clip the remove_rect against the rects contained within the tree //-check the remove_rect against each node starting with the parent. //-if the remove_rect interects or is contained wihin a child area walk down that // childs nodes // a)if intersect, walk down child node // b)if contained, walk only down this child node, all others can be ignored //-if the remove_rect contains any rects , reomve them from the list //-if the remove_rect overlaps any of the rects, split them into new rects, remove the ones which // are contained within the remove rect, add the new splitable rects (might be able to sort down // to increase efficency) bool can_modify_source = false; int inital_depth = 0; t_screen_rect local_rect = remove_rect_area; m_global_free_rect_list->clear_rect_list(m_split_list); //if the area is large enough its better to perform a linear search and resort the tree if (((remove_rect_area.width()*remove_rect_area.height()) > m_remove_threshold || m_rects.size() < k_remove_rect_count_threshold) && g_enable_flag) { t_rect_list::iterator rect_iterator = m_rects.begin(); t_rect_list::iterator next_rect_iterator; m_global_free_rect_list->clear_rect_list(m_split_list); while (rect_iterator != m_rects.end()) { next_rect_iterator = rect_iterator; next_rect_iterator++; if (::intersect(local_rect, *rect_iterator) == false) { rect_iterator = next_rect_iterator; continue; } if (split_rect( local_rect, *rect_iterator, m_split_list ) == true) m_global_free_rect_list->add_to_free_list(m_rects,rect_iterator); rect_iterator = next_rect_iterator; } m_rects.splice(m_rects.begin(),m_split_list,m_split_list.begin(),m_split_list.end()); resort_all(); m_global_free_rect_list->clear_rect_list(m_split_list); return; } //split the tree against the rect split_tree_against_rect( m_parent_node, local_rect, m_split_list, inital_depth ); //reinsert the split rects into the tree int split_size = m_split_list.size(); if (!m_split_list.empty()) { std::list<t_screen_rect>::iterator iter; for (iter = m_split_list.begin(); iter != m_split_list.end(); iter++) { if (iter->width() > 0 && iter->height() > 0) { t_quadtree_node* insert_split_node = find_insert_node( m_parent_node, *iter, inital_depth ); add_rect( *insert_split_node, *iter); } } } m_global_free_rect_list->clear_rect_list(m_split_list); return; }
void t_sorted_clip_list::insert( t_screen_rect const & insert_rect_var, bool splitable ) { //put the insert_rect into the tree //-check the insert_rect against each node starting with the parent. //-if the insert_rect interects or is contained wihin a child area walk down that // childs nodes // a)if intersect, walk down child node // b)if contained, walk only down this child node, all others can be ignored //-if the insert_rect contains any rects , reomve them from the list //-if the insert_rect overlaps any of the rects splitable them into new rects bool can_insert_rect = true; int inital_depth = 0; t_screen_rect local_rect = insert_rect_var; m_global_free_rect_list->clear_rect_list(m_split_list); if (((insert_rect_var.width()*insert_rect_var.height()) > m_insert_threshold || m_rects.size() < k_insert_rect_count_threshold ) && g_enable_flag) { t_rect_list::iterator rect_iterator = m_rects.begin(); t_rect_list::iterator next_rect_iterator; while (rect_iterator != m_rects.end()) { next_rect_iterator = rect_iterator; next_rect_iterator++; if (::intersect(local_rect, *rect_iterator) == false) { rect_iterator = next_rect_iterator; continue; } if ( rect_iterator->contains(local_rect) ) { return; } if (local_rect.contains(*rect_iterator) == true) { m_global_free_rect_list->add_to_free_list(m_rects,rect_iterator); rect_iterator = next_rect_iterator; continue; } if (split_rect(local_rect, *rect_iterator, m_split_list ) == true) { //remove this rect from the list as it has been split m_global_free_rect_list->add_to_free_list(m_rects,rect_iterator); rect_iterator = next_rect_iterator; continue; } rect_iterator = next_rect_iterator; } merge_rects_with(local_rect, m_split_list); m_global_free_rect_list->push_back_use_free_list(m_rects,local_rect); m_rects.splice(m_rects.begin(),m_split_list,m_split_list.begin(),m_split_list.end()); resort_all(); return; } //split the tree against the rect insert_rect_into_tree( m_parent_node, local_rect, m_split_list, inital_depth, can_insert_rect ); //reinsert the split rects into the tree if (can_insert_rect) { merge_rects_with(local_rect, m_split_list); if (m_split_list.size() != 0) { std::list<t_screen_rect>::iterator iter; t_quadtree_node* insert_split_node; for (iter = m_split_list.begin(); iter != m_split_list.end(); iter++) { if (iter->width() > 0 && iter->height() > 0) { insert_split_node = find_insert_node( m_parent_node, *iter, inital_depth ); add_rect( *insert_split_node, *iter); } } } } //add the new rect into the tree //find the insertion node if (can_insert_rect) { if (local_rect.width() > 0 && local_rect.height() > 0) { t_quadtree_node* insert_node = find_insert_node( m_parent_node, local_rect, inital_depth); add_rect( *insert_node, local_rect); } } m_global_free_rect_list->clear_rect_list(m_split_list); return; }
int main(int argc, char *argv[]) { int mouse_begin_x = -1, mouse_begin_y = -1; int i, done; SDL_Event event; Uint32 then, now, frames; /* Initialize parameters */ num_objects = NUM_OBJECTS; /* Initialize test framework */ state = CommonCreateState(argv, SDL_INIT_VIDEO); if (!state) { return 1; } for (i = 1; i < argc;) { int consumed; consumed = CommonArg(state, i); if (consumed == 0) { consumed = -1; if (SDL_strcasecmp(argv[i], "--blend") == 0) { if (argv[i + 1]) { if (SDL_strcasecmp(argv[i + 1], "none") == 0) { blendMode = SDL_BLENDMODE_NONE; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) { blendMode = SDL_BLENDMODE_MASK; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { blendMode = SDL_BLENDMODE_BLEND; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { blendMode = SDL_BLENDMODE_ADD; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { blendMode = SDL_BLENDMODE_MOD; consumed = 2; } } } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { cycle_color = SDL_TRUE; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { cycle_alpha = SDL_TRUE; consumed = 1; } else if (SDL_isdigit(*argv[i])) { num_objects = SDL_atoi(argv[i]); consumed = 1; } } if (consumed < 0) { fprintf(stderr, "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n", argv[0], CommonUsage(state)); return 1; } i += consumed; } if (!CommonInit(state)) { return 2; } /* Create the windows and initialize the renderers */ for (i = 0; i < state->num_windows; ++i) { SDL_SelectRenderer(state->windows[i]); SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(); } srand(time(NULL)); /* Main render loop */ frames = 0; then = SDL_GetTicks(); done = 0; while (!done) { /* Check for events */ ++frames; while (SDL_PollEvent(&event)) { CommonEvent(state, &event, &done); switch (event.type) { case SDL_MOUSEBUTTONDOWN: if (event.button.which == 0) { mouse_begin_x = event.button.x; mouse_begin_y = event.button.y; } break; case SDL_MOUSEBUTTONUP: if (event.button.which == 0) { if (event.button.button == 3) add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); if (event.button.button == 1) add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); } break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case 'l': if (event.key.keysym.mod & KMOD_SHIFT) num_lines = 0; else add_line(rand() % 640, rand() % 480, rand() % 640, rand() % 480); break; case 'r': if (event.key.keysym.mod & KMOD_SHIFT) num_rects = 0; else add_rect(rand() % 640, rand() % 480, rand() % 640, rand() % 480); break; } break; case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_EXPOSED: SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID)); SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(); break; } break; default: break; } } for (i = 0; i < state->num_windows; ++i) { SDL_SelectRenderer(state->windows[i]); SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(); DrawRects(state->windows[i]); DrawPoints(state->windows[i]); DrawRectRectIntersections(state->windows[i]); DrawLines(state->windows[i]); DrawRectLineIntersections(state->windows[i]); SDL_RenderPresent(); } } CommonQuit(state); /* Print out some timing information */ now = SDL_GetTicks(); if (now > then) { double fps = ((double) frames * 1000) / (now - then); printf("%2.2f frames per second\n", fps); } return 0; }
void loop() { int i; SDL_Event event; /* Check for events */ while (SDL_PollEvent(&event)) { SDLTest_CommonEvent(state, &event, &done); switch (event.type) { case SDL_MOUSEBUTTONDOWN: mouse_begin_x = event.button.x; mouse_begin_y = event.button.y; break; case SDL_MOUSEBUTTONUP: if (event.button.button == 3) add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); if (event.button.button == 1) add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case 'l': if (event.key.keysym.mod & KMOD_SHIFT) num_lines = 0; else add_line(rand() % 640, rand() % 480, rand() % 640, rand() % 480); break; case 'r': if (event.key.keysym.mod & KMOD_SHIFT) num_rects = 0; else add_rect(rand() % 640, rand() % 480, rand() % 640, rand() % 480); break; } break; default: break; } } for (i = 0; i < state->num_windows; ++i) { SDL_Renderer *renderer = state->renderers[i]; if (state->windows[i] == NULL) continue; SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(renderer); DrawRects(renderer); DrawPoints(renderer); DrawRectRectIntersections(renderer); DrawLines(renderer); DrawRectLineIntersections(renderer); SDL_RenderPresent(renderer); } #ifdef __EMSCRIPTEN__ if (done) { emscripten_cancel_main_loop(); } #endif }