How To Explore Using Camera

This how to demonstrate how to navigate around the virtual world using SwinGame’s Camera.

Level

Intermediate

Details

In order to be able to navigate around the virtual world, register key for input is a must. The KeyDown or KeyTyped Procedure can be used for this purpose.

Once the key have been registered for input, MoveCameraBy procedure can be used to move the camera around the virtual world. To focus onto a certain point on the screen MoveCameraTo can be used.

A short video clip will be display on the right showing the program created from the code below, demonstrating the use of MoveCameraBy and MoveCameraTo. In this example MoveCameraTo will be move to a world location 0,0 when the user press key 0.


Source Code Widget

  • program HowToExploreUsingCamera;
    uses SwinGame,sgTypes;
    
    procedure Main();
    
    begin
        OpenGraphicsWindow('Explore with camera' ,800 ,600 );
        LoadDefaultColors();
        repeat
            ProcessEvents();
            if  KeyDown(vk_UP )    then
                MoveCameraBy(0 ,-1 );
            if  KeyDown(vk_DOWN )    then
                MoveCameraBy(0 ,+1 );
            if  KeyDown(vk_LEFT )    then
                MoveCameraBy(-1 ,0 );
            if  KeyDown(vk_RIGHT )    then
                MoveCameraBy(+1 ,0 );
            if  KeyTyped(vk_0 )    then
                MoveCameraTo(0 ,0 );
            ClearScreen(ColorWhite );
            FillRectangle(RGBColor(205 ,201 ,201 )  ,-150 ,250 ,1150 ,20 );
            FillRectangle(RGBColor(205 ,201 ,201 )  ,-150 ,330 ,1150 ,20 );
            FillRectangle(RGBColor(255 ,255 ,0 )  ,-150 ,290 ,50 ,20 );
            FillRectangle(RGBColor(124 ,252 ,0 )  ,-50 ,290 ,50 ,20 );
            FillRectangle(RGBColor(184 ,134 ,11 )  ,50 ,290 ,50 ,20 );
            FillRectangle(RGBColor(0 ,255 ,255 )  ,150 ,290 ,50 ,20 );
            FillRectangle(RGBColor(255 ,165 ,0 )  ,250 ,290 ,50 ,20 );
            FillRectangle(RGBColor(255 ,192 ,203 )  ,350 ,290 ,50 ,20 );
            FillRectangle(RGBColor(160 ,32 ,240 )  ,450 ,290 ,50 ,20 );
            FillRectangle(RGBColor(165 ,42 ,42 )  ,550 ,290 ,50 ,20 );
            FillRectangle(RGBColor(240 ,230 ,140 )  ,650 ,290 ,50 ,20 );
            FillRectangle(RGBColor(0 ,0 ,128 )  ,750 ,290 ,50 ,20 );
            FillRectangle(RGBColor(245 ,255 ,250 )  ,850 ,290 ,50 ,20 );
            FillRectangle(RGBColor(255 ,228 ,225 )  ,950 ,290 ,50 ,20 );
            DrawCircle(ColorBlue ,105 ,420 ,30 );
            FillRectangle(ColorBlack ,100 ,450 ,10 ,50 );
            FillEllipse(ColorBlue ,300 ,50 ,60 ,30 );
            DrawText('Ellipse in blue at position 300,50' ,ColorRed ,380 ,60 );
            DrawTextOnScreen(PointToString(CameraPos()  )  ,ColorBlack ,0 ,0 );
            RefreshScreen();
        
        until WindowCloseRequested();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
    
        open_graphics_window("Explore with camera" ,800 ,600 );
        load_default_colors();
        do 
        {
            process_events();
            if ( key_down(VK_UP )  )
                move_camera_by(0 ,-1 );
            if ( key_down(VK_DOWN )  )
                move_camera_by(0 ,+1 );
            if ( key_down(VK_LEFT )  )
                move_camera_by(-1 ,0 );
            if ( key_down(VK_RIGHT )  )
                move_camera_by(+1 ,0 );
            if ( key_typed(VK_0 )  )
                move_camera_to(0 ,0 );
            clear_screen(color_white );
            fill_rectangle(rgbcolor(205 ,201 ,201 ) ,-150 ,250 ,1150 ,20 );
            fill_rectangle(rgbcolor(205 ,201 ,201 ) ,-150 ,330 ,1150 ,20 );
            fill_rectangle(rgbcolor(255 ,255 ,0 ) ,-150 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(124 ,252 ,0 ) ,-50 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(184 ,134 ,11 ) ,50 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(0 ,255 ,255 ) ,150 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(255 ,165 ,0 ) ,250 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(255 ,192 ,203 ) ,350 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(160 ,32 ,240 ) ,450 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(165 ,42 ,42 ) ,550 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(240 ,230 ,140 ) ,650 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(0 ,0 ,128 ) ,750 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(245 ,255 ,250 ) ,850 ,290 ,50 ,20 );
            fill_rectangle(rgbcolor(255 ,228 ,225 ) ,950 ,290 ,50 ,20 );
            draw_circle(color_blue ,105 ,420 ,30 );
            fill_rectangle(color_black ,100 ,450 ,10 ,50 );
            fill_ellipse(color_blue ,300 ,50 ,60 ,30 );
            draw_text("Ellipse in blue at position 300,50" ,color_red ,380 ,60 );
            draw_text_on_screen(point_to_string(camera_pos() ) ,color_black ,0 ,0 );
            refresh_screen();
        }
        while ( ! (window_close_requested() ) );
        release_all_resources();
        return 0;
    }
    
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

Possibilities

  • Allow you to use the camera to create a virtual world for platform or side scrolling game such as Super Mario Bros series or Castlevania series.