bool game_init() { Advanced2D::Sprite* background = new Advanced2D::Sprite(); if (!background->loadImage(TEXT("galaxies.tga"))) { Advanced2D::Engine::showFatalMessage(TEXT("Unable to load galaxies.tga image"), TEXT("game_init")); return false; } background->setObjectType(OBJECT_BACKGROUND); background->setCollidable(false); g_engine->addEntity(background); console = new Advanced2D::Console(); console->setShowing(true); try { circle_image = new Advanced2D::Texture(TEXT("circle.tga"), D3DCOLOR_XRGB(0, 0, 0)); } catch (const std::exception&) { Advanced2D::Engine::showFatalMessage(TEXT("Unable to load circle.tga image"), TEXT("game_init")); return false; } for (int n = 0; n < MAX_PR; ++n) { add_sprite(); } try { font = new Advanced2D::Font(TEXT("verdana10.dat")); } catch (const std::exception&) { Advanced2D::Engine::showFatalMessage(TEXT("Unable to load verdana10.dat file"), TEXT("game_init")); return false; } if (!font->loadImage(TEXT("verdana10.tga"))) { Advanced2D::Engine::showFatalMessage(TEXT("Unable to load verdana10.tga image"), TEXT("game_init")); return false; } font->setColumns(16); font->setSize(20, 16); g_engine->setMaximizeProcessor(true); return true; }
t_dlist *init_sprites(t_mega *all) { t_dlist *list; list = NULL; add_sprite(&list, CAGE, 9.5, 1.5); add_sprite(&list, CAGE2, 1.5, 5.5); add_sprite(&list, BARREL, 4.5, 4.3); add_sprite(&list, BARREL, 4.2, 4.6); add_sprite(&list, KNIGHT, 7.5, 12.5); add_sprite(&list, KNIGHT, 7.5, 19.5); add_sprite(&list, KNIGHT, 15.5, 22.5); add_sprite(&list, KNIGHT, 12.5, 22.5); add_sprite(&list, KNIGHT, 15.5, 10.5); init_sprite2(all, list); return (list); }
void create_game(ng::i_layout& aLayout) { auto spritePlane = std::make_shared<ng::sprite_plane>(); aLayout.add_item(spritePlane); spritePlane->set_font(ng::font(spritePlane->font(), ng::font::Bold, 28)); spritePlane->set_background_colour(ng::colour::Black); spritePlane->enable_z_sorting(true); for (uint32_t i = 0; i < 1000; ++i) spritePlane->add_shape(std::make_shared<ng::rectangle>( *spritePlane, ng::vec3{static_cast<ng::scalar>(std::rand() % 800), static_cast<ng::scalar>(std::rand() % 800), -(static_cast<ng::scalar>(std::rand() % 32))}, ng::size{static_cast<ng::scalar>(std::rand() % 64), static_cast<ng::scalar>(std::rand() % 64)}, ng::colour(std::rand() % 64, std::rand() % 64, std::rand() % 64))); //spritePlane->set_uniform_gravity(); //spritePlane->set_gravitational_constant(0.0); //spritePlane->create_earth(); auto& spaceshipSprite = spritePlane->create_sprite(ng::image(sSpaceshipImagePattern, { {0, ng::colour()}, {1, ng::colour::LightGoldenrod}, {2, ng::colour::DarkGoldenrod4} })); spaceshipSprite.physics().set_mass(1.0); spaceshipSprite.set_size(ng::size(36.0, 36.0)); spaceshipSprite.set_position_3D(ng::vec3(400.0, 18.0, 1.0)); auto shipInfo = std::make_shared<ng::text>(*spritePlane, ng::vec3{}, "", ng::font("SnareDrum One NBP", "Regular", 24.0), ng::colour::White); shipInfo->set_border(1.0); shipInfo->set_margins(ng::margins(2.0)); shipInfo->set_buddy(spaceshipSprite, ng::vec3{18.0, 18.0, 0.0}); spritePlane->add_shape(shipInfo); spritePlane->sprites_painted([spritePlane](ng::graphics_context& aGraphicsContext) { aGraphicsContext.draw_text(ng::point(0.0, 0.0), "Hello, World!", spritePlane->font(), ng::colour::White); }); spritePlane->physics_applied([spritePlane, &spaceshipSprite, shipInfo]() { const auto& keyboard = ng::app::instance().keyboard(); spaceshipSprite.physics().set_acceleration({ keyboard.is_key_pressed(ng::ScanCode_RIGHT) ? 16.0 : keyboard.is_key_pressed(ng::ScanCode_LEFT) ? -16.0 : 0.0, keyboard.is_key_pressed(ng::ScanCode_UP) ? 16.0 : keyboard.is_key_pressed(ng::ScanCode_DOWN) ? -16.0 : 0.0 }); if (keyboard.is_key_pressed(ng::ScanCode_X)) spaceshipSprite.physics().set_spin_degrees(30.0); else if (keyboard.is_key_pressed(ng::ScanCode_Z)) spaceshipSprite.physics().set_spin_degrees(-30.0); else spaceshipSprite.physics().set_spin_degrees(0.0); if (keyboard.is_key_pressed(ng::ScanCode_SPACE)) spritePlane->add_sprite(std::make_shared<bullet>(spaceshipSprite)); std::ostringstream oss; oss << "VELOCITY: " << spaceshipSprite.physics().velocity().magnitude() << " m/s" << "\n"; oss << "ACCELERATION: " << spaceshipSprite.physics().acceleration().magnitude() << " m/s/s"; shipInfo->set_value(oss.str()); }); }
void test_loop() { U8 exit_flag=0; init_game(); while(!exit_flag) { swap_screens(); /* swap physic<->logic */ restore_sprites(); /* restore sprite list */ do_ikbd(); /* process keyboard input */ add_sprite(&my_sprites[0]); /* add sprite1 to list */ draw_sprites(); /* draw sprite list */ if(rlkb_test_key(0x44)) exit_flag=1; /* exit if F10 pressed */ } }
void game_update() { add_sprite(); updateConsole(); }
void bottom_panel::on_init() { h_screen_paradigm scr_paradigm = get_framework()->get_screen_paradigm(); h_string suffix = get_framework()->get_screen_paradigm_suffix(scr_paradigm); h_sprite_ptr panel = add_sprite("/images/panels/panel_bottom" + suffix, "base"); h_rect rc = get_framework()->get_view()->get_rect(); float width = rc.get_width(); set_size(h_vector(width, panel->get_size().y)); float scale_horiz_factor = get_size().x / panel->get_size().x; h_logger::info(h_string("[bottom_panel] scale_horiz_factor : %f", scale_horiz_factor)); //panel->set_pos(h_vector(10, 0)); //panel->set_scale_h(scale_horiz_factor); set_scale(h_vector(scale_horiz_factor, 1)); // init bottom widgets // balls.. { m_button_balls.reset(new h_button("button_balls_0")); add_child(m_button_balls); scene_manager* scene_manager = get_ctrl()->get_scene_manager(); static const float width[3] = { 48, 81, 95 }; static const float height[3] = { 48, 81, 95 }; h_rect rc = scene_manager->get_rect_from_atlas((int)width[scr_paradigm], (int)height[scr_paradigm], 4, 4, 0); m_button_balls->load_images("/images/dlg/balls_set_0" + suffix, rc.get_top_left(), rc.get_bottom_right(), rc.get_top_left(), rc.get_bottom_right()); m_button_balls->set_action(std::tr1::bind(&scene_manager::on_select_toys_dlg, get_ctrl()->get_scene_manager(), dlg_balls)); } // staff.. { m_button_staff.reset(new h_button("button_staff_0")); add_child(m_button_staff); scene_manager* scene_manager = get_ctrl()->get_scene_manager(); static const float width[3] = { 48, 81, 95 }; static const float height[3] = { 48, 81, 95 }; h_rect rc = scene_manager->get_rect_from_atlas((int)width[scr_paradigm], (int)height[scr_paradigm], 4, 4, 0); m_button_staff->load_images("/images/dlg/staff_set_0" + suffix, rc.get_top_left(), rc.get_bottom_right(), rc.get_top_left(), rc.get_bottom_right()); m_button_staff->set_action(std::tr1::bind(&scene_manager::on_select_toys_dlg, get_ctrl()->get_scene_manager(), dlg_staff)); } // undo.. { m_button_undo.reset(new h_button("button_undo_0")); add_child(m_button_undo); scene_manager* scene_manager = get_ctrl()->get_scene_manager(); static const float width[3] = { 48, 81, 95 }; static const float height[3] = { 48, 81, 95 }; m_button_undo->load_images("/images/buttons/undo" + suffix, "/images/buttons/undo" + suffix); m_button_undo->set_action(std::tr1::bind(&scene_manager::on_undo, get_ctrl()->get_scene_manager())); } // menu.. { m_button_menu.reset(new h_button("menu")); add_child(m_button_menu); scene_manager* scene_manager = get_ctrl()->get_scene_manager(); static const float width[3] = { 60, 102, 120 }; static const float height[3] = { 48, 81, 95 }; m_button_menu->load_images("/images/buttons/menu" + suffix, "/images/buttons/menu" + suffix); m_button_menu->set_action(std::tr1::bind(&scene_manager::on_menu, get_ctrl()->get_scene_manager())); } align_buttons(); }
static void add_sprites(int n) { int i; for (i = 0; i < n; i++) add_sprite(); }
int main(void) { ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_MONITOR_INFO info; int w = 640, h = 480; bool done = false; bool need_redraw = true; bool background = false; example.show_help = true; if (!al_init()) { abort_example("Failed to init Allegro.\n"); return 1; } if (!al_init_image_addon()) { abort_example("Failed to init IIO addon.\n"); return 1; } al_init_font_addon(); al_get_num_video_adapters(); al_get_monitor_info(0, &info); #ifdef ALLEGRO_IPHONE al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); #endif al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_ALL, ALLEGRO_SUGGEST); example.display = al_create_display(w, h); w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!example.display) { abort_example("Error creating display.\n"); return 1; } if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); return 1; } if (!al_install_mouse()) { abort_example("Error installing mouse.\n"); return 1; } example.font = al_load_font("data/fixed_font.tga", 0, 0); if (!example.font) { abort_example("Error loading data/fixed_font.tga\n"); return 1; } example.mysha = al_load_bitmap("data/mysha256x256.png"); if (!example.mysha) { abort_example("Error loading data/mysha256x256.png\n"); return 1; } example.white = al_map_rgb_f(1, 1, 1); example.half_white = al_map_rgba_f(1, 1, 1, 0.5); example.dark = al_map_rgb(15, 15, 15); example.red = al_map_rgb_f(1, 0.2, 0.1); change_size(256); add_sprite(); add_sprite(); timer = al_create_timer(1.0 / FPS); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); if (al_install_touch_input()) al_register_event_source(queue, al_get_touch_input_event_source()); al_register_event_source(queue, al_get_display_event_source(example.display)); al_start_timer(timer); while (!done) { float x, y; ALLEGRO_EVENT event; w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!background && need_redraw && al_is_event_queue_empty(queue)) { double t = -al_get_time(); add_time(); al_clear_to_color(al_map_rgb_f(0, 0, 0)); redraw(); t += al_get_time(); example.direct_speed_measure = t; al_flip_display(); need_redraw = false; } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_CHAR: /* includes repeats */ if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; else if (event.keyboard.keycode == ALLEGRO_KEY_UP) { add_sprites(1); } else if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) { remove_sprites(1); } else if (event.keyboard.keycode == ALLEGRO_KEY_LEFT) { change_size(example.bitmap_size - 1); } else if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT) { change_size(example.bitmap_size + 1); } else if (event.keyboard.keycode == ALLEGRO_KEY_F1) { example.show_help ^= 1; } else if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { example.use_memory_bitmaps ^= 1; change_size(example.bitmap_size); } else if (event.keyboard.keycode == ALLEGRO_KEY_B) { example.blending++; if (example.blending == 4) example.blending = 0; } break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING: background = true; al_acknowledge_drawing_halt(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING: background = false; break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_TIMER: update(); need_redraw = true; break; case ALLEGRO_EVENT_TOUCH_BEGIN: x = event.touch.x; y = event.touch.y; goto click; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: x = event.mouse.x; y = event.mouse.y; goto click; click: { int fh = al_get_font_line_height(example.font); if (x < 80 && y >= h - fh * 10) { int button = (y - (h - fh * 10)) / (fh * 2); if (button == 0) { example.use_memory_bitmaps ^= 1; change_size(example.bitmap_size); } if (button == 1) { example.blending++; if (example.blending == 4) example.blending = 0; } if (button == 3) { if (x < 40) remove_sprites(example.sprite_count / 2); else add_sprites(example.sprite_count); } if (button == 2) { int s = example.bitmap_size * 2; if (x < 40) s = example.bitmap_size / 2; change_size(s); } if (button == 4) { example.show_help ^= 1; } } break; } } } al_destroy_bitmap(example.bitmap); return 0; }
static void init_sprite2(t_mega *all, t_dlist *list) { add_sprite(&list, LIGHT, 2, 18.5); add_sprite(&list, PUIT, 17.5, 19.5); add_sprite(&list, PUIT, 17.5, 13.5); add_sprite(&list, PILAR, 15.5, 19.5); add_sprite(&list, PILAR, 15.5, 13.5); add_sprite(&list, PILAR, 12.5, 19.5); add_sprite(&list, PILAR, 12.5, 13.5); add_sprite(&list, PILAR, 15.5, 6.5); add_sprite(&list, PILAR, 13.5, 6.5); add_sprite(&list, PILAR, 5.5, 14.5); add_sprite(&list, PILAR, 5.5, 18.5); add_sprite(&list, CAGE6, 14.5, 1.2); add_sprite(&list, DEAD, 16.9, 3.7); add_sprite(&list, TABLE3, 16.5, 4.6); add_sprite(&list, TABLE2, 13.5, 3.5); add_sprite(&list, LIGHT, 14.5, 2.5); add_sprite(&list, SINK, 17.5, 1.5); add_sprite(&list, CAGE3, 12.5, 4.5); add_sprite(&list, CAGE4, 13.5, 4.5); add_sprite(&list, CAGE5, 12.5, 1.5); add_sprite(&list, CUBE, 17.5, 24.8); add_sprite(&list, CAKE, 17.2, 24.3); add_sprite(&list, LIGHT, 16.5, 24.5); add_sprite(&list, CHEST, 6.5, 30.5); init_sprite3(all, list); }
static void init_sprite3(t_mega *all, t_dlist *list) { add_sprite(&list, KNIGHT, 12.5, 10.5); add_sprite(&list, KNIGHT, 4.6, 2.4); add_sprite(&list, BARREL, 2.5, 11.5); add_sprite(&list, BARREL, 2, 12); add_sprite(&list, BARREL, 1.5, 11.5); add_sprite(&list, AMMO, 1.5, 20.5); add_sprite(&list, GUN_LOOT, 2.5, 20.5); add_sprite(&list, BLOOD, 3.5, 5.5); add_sprite(&list, BLOOD, 7.5, 8.5); add_sprite(&list, KEY, 4.3, 6.5); add_sprite(&list, KEY, 17.5, 3.9); add_sprite(&list, TABLE, 9, 3.5); add_sprite(&list, LIGHT, 7.5, 3); add_sprite(&list, LIGHT, 3.5, 9.5); add_sprite(&list, LIGHT, 17.5, 9.5); add_sprite(&list, LIGHT, 14.5, 7.5); add_sprite(&list, LIGHT, 2, 14.5); add_sprite(&list, CHEST, 4.5, 33.5); }
int main(int argc, char* argv[]) { GPU_Target* screen; printRenderers(); screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); if(screen == NULL) return -1; printCurrentRenderer(); { Uint32 startTime; long frameCount; Uint8 done; SDL_Event event; GPU_Image* image; #define MAX_SPRITES 100 int numSprites; float positions[2*MAX_SPRITES]; float colors[4*4*MAX_SPRITES]; float expanded_colors[4*MAX_SPRITES]; float src_rects[4*MAX_SPRITES]; Uint32 v, f, p; GPU_ShaderBlock block; Uint8 shader_index; int i; SDL_Color color = {255, 255, 255, 255}; SDL_Color red = {255, 0, 0, 255}; SDL_Color green = {0, 255, 0, 255}; SDL_Color blue = {0, 0, 255, 255}; GPU_Rect src_rect; int mx, my; Uint32 mouse_state; image = GPU_LoadImage("data/happy_50x50.bmp"); if(image == NULL) return -1; numSprites = 0; color_attr.format = GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, 4*sizeof(float), 0); color_attr.format.is_per_sprite = 0; color_attr.values = colors; block = load_shaders(&v, &f, &p); shader_index = 1; set_shader(p, &block); startTime = SDL_GetTicks(); frameCount = 0; src_rect.x = 0; src_rect.y = 0; src_rect.w = image->w; src_rect.h = image->h; add_sprite(positions, colors, expanded_colors, src_rects, &numSprites, color, src_rect); done = 0; while(!done) { while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) done = 1; else if(event.type == SDL_MOUSEBUTTONDOWN) { if(event.button.x <= 150 && event.button.y <= 150) { if(event.button.button == SDL_BUTTON_LEFT) { float dx = event.button.x/3 - src_rect.x; float dy = event.button.y/3 - src_rect.y; src_rect.x = event.button.x/3; src_rect.y = event.button.y/3; src_rect.w -= dx; src_rect.h -= dy; } else if(event.button.button == SDL_BUTTON_RIGHT) { src_rect.w = event.button.x/3 - src_rect.x; src_rect.h = event.button.y/3 - src_rect.y; } } } else if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE) done = 1; else if(event.key.keysym.sym == SDLK_EQUALS || event.key.keysym.sym == SDLK_PLUS) { if(numSprites < MAX_SPRITES) add_sprite(positions, colors, expanded_colors, src_rects, &numSprites, color, src_rect); } else if(event.key.keysym.sym == SDLK_MINUS) { if(numSprites > 0) numSprites--; } else if(event.key.keysym.sym == SDLK_SPACE) { shader_index++; shader_index %= 2; if(shader_index == 0) set_shader(0, NULL); else if(shader_index == 1) set_shader(p, &block); } else if(event.key.keysym.sym == SDLK_RETURN) { use_color_expansion = !use_color_expansion; if(use_color_expansion) { GPU_LogError("Using attribute expansion.\n"); color_attr.format.is_per_sprite = 1; color_attr.values = expanded_colors; } else { GPU_LogError("Using per-vertex attributes.\n"); color_attr.format.is_per_sprite = 0; color_attr.values = colors; } } } } mouse_state = SDL_GetMouseState(&mx, &my); if(mouse_state & (SDL_BUTTON_LMASK | SDL_BUTTON_RMASK)) { if(mx <= 150 && my <= 150) { if(mouse_state & SDL_BUTTON_LMASK) { float dx = mx/3 - src_rect.x; float dy = my/3 - src_rect.y; src_rect.x = mx/3; src_rect.y = my/3; src_rect.w -= dx; src_rect.h -= dy; } else if(mouse_state & SDL_BUTTON_RMASK) { src_rect.w = mx/3 - src_rect.x; src_rect.h = my/3 - src_rect.y; } } } GPU_SetUniformf(timeloc, SDL_GetTicks()/1000.0f); GPU_Clear(screen); if(use_color_expansion) GPU_SetAttributeSource(numSprites, color_attr); else GPU_SetAttributeSource(4*numSprites, color_attr); for(i = 0; i < numSprites; i++) { GPU_Rect r = {src_rects[4*i], src_rects[4*i+1], src_rects[4*i+2], src_rects[4*i+3]}; GPU_Blit(image, &r, screen, positions[2*i], positions[2*i+1]); } //GPU_BlitBatchSeparate(image, screen, numSprites, positions, src_rects, expanded_colors, 0); set_shader(0, NULL); GPU_BlitScale(image, NULL, screen, 75, 75, 3.0f, 3.0f); GPU_Rectangle(screen, 3*src_rect.x, 3*src_rect.y, 3*(src_rect.x + src_rect.w), 3*(src_rect.y + src_rect.h), red); GPU_CircleFilled(screen, 3*src_rect.x, 3*src_rect.y, 4, blue); GPU_CircleFilled(screen, 3*(src_rect.x + src_rect.w), 3*(src_rect.y + src_rect.h), 4, green); if(shader_index == 1) set_shader(p, &block); GPU_Flip(screen); frameCount++; if(frameCount%500 == 0) printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); } printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); GPU_FreeImage(image); free_shaders(v, f, p); } GPU_Quit(); return 0; }