Tutorial:Mode7 demo

From Bennu Wiki
Jump to: navigation, search


Introduction

This is a mode7 tutorial made by ruckage, as posted on these forum topics: Mode 7 Demonstration program and Mode 7 problems - possible bugs.

Downloads

You can download an archive here


The program

This program uses graphics that are drawn by bennu's Mod_draw functions. The progam is pretty self explanitory and everything is extensively commented.


//******* Mode 7 Tutorial program by ruckage *******

program mode_7;

import "mod_m7"
//import "mod_screen"
import "mod_proc"
import "mod_video"
import "mod_text"
import "mod_map"
import "mod_draw"
import "mod_math"
import "mod_grproc"
import "mod_key"
import "mod_wm"

global
texture_1;
texture_2;
ball;

c; //used to store color values when generating the textures

begin
set_mode(320,240,16);
set_fps(60,0);


//// make some images to display folded on the mode_7 plane
texture_1=new_map(1024,1024,16); /// this will be the main image used
texture_2=new_map(128,128,16); /// this will be used to fill the plane outside the main image
ball=new_map(512,512,16); /// this will be used to create a ball sprite


    for(x=0;x<1024;x++);
        for(y=0;y<1024;y++);
            c=(x^y)%256;        
            map_put_pixel(0,texture_1,x,y,rgb(128+(c%128),x/4,y/4));  /// This creates a pretty xor pattern
        end
    end

    for(x=0;x<128;x++)
        for(y=0;y<128;y++)
        c=(x^y)*2;        
        map_put_pixel(0,texture_2,x,y,rgb(c,64,c%128));  /// This creates a another xor pattern
        end
    end

    for(x=128;x>0;x-=16)
    //// draw a ball sprite
    drawing_map(0,ball);
    drawing_color(rgb(128-x,255-x,255-x));
    draw_fcircle(128,128,x);
    end


//// the next section sets up a mode_7 window
//// I will setup 2 windows, 1 for use as the floor and 1 as a ceiling
/// syntax: start_mode7(m7_id,file,main_graph,external_graph,region,horizon)

start_mode7(0,0,texture_1,texture_2,0,0); /// this will be the floor
start_mode7(1,0,texture_1,texture_2,0,0); /// this will be the ceiling

m7[0].camera=player(); //// creates the player process and sets the m7[0].camera to it's id.  
                       //// Camera has to be set to a process ID for mode 7 to be seen.
m7[0].height=50;  //// The height of the camera.  Positive values above plane, negative values below plane.
m7[0].focus=240;  //adjust this to fit various reolutions.  240 optimal for 320x240 display, 480 optimal for 640x480 display etc.
m7[0].distance=64; /// sets the distance the camera is placed behind the process previously set with m7.camera

//// second mode 7 plane used as ceiling - most values are set to match m7[0]
m7[1].camera=m7[0].camera;
m7[1].height=-50; /// negative height means the camera is placed below the plane - this gives us our ceiling
m7[1].focus=m7[0].focus;
m7[0].distance=m7[0].distance;




///populate the mode_7 plane with some objects

    for(x=0;x<1024;x+=64)
        for(y=0;y<1024;y+=64)
        mode7_object(x,y);
        end
    end
    
    loop
        if(exit_status!=0)exit();end
        frame;
    end

end
//******* END OF MAIN PROGRAM *******
//***********************************





// **** Player process.  The mode 7 camera is set to follow this process

process player()

begin

resolution=100;

    loop

        if(key(_up))advance(300);end  // up key move forward
        if(key(_down))advance(-300);end  // down key move backward
        if(key(_left))angle+=1000;end  // left key turn left
        if(key(_right))angle-=1000;end  // right key turn right


    frame;
    end

end



// **** Process for Mode_7 objects


process mode7_object(x,y)

private
phase;

    begin

    ctype=c_m7; //// setting ctype to c_m7 tells the process that it's image is to be positioned and scaled 
                //// within the mode 7 window
    cnumber=1; //// this sets which mode 7 plane the process is rendered in.  Set to 0 it will render duplicate 
               //// copies on all active mode_7 planes at once.  Especially useful for splitscreen multiplayer.

    graph=ball;
    phase=(x+y)*300;


        loop

        height=50+(sin(phase)*50);
        phase+=1500;

        frame;
        end

end



Used in example: key(), map_put_pixel(), drawing_map(), drawing_color(), rgb(), draw_fcircle(), Start_mode7(), mode7 structure, Xor