ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
main.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # (c) 2009 Lion
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
11 #include <kernel.h>
12 #include <libgs.h>
13 
14 // with double buffering the DMA can be transferig data to
15 // the GS while you are making/calculating data for the next
16 // frame which speed things up. you dont have to wait on DMA to finish
17 // transfering data to the GS
18 
19 #define SCREEN_WIDTH 640
20 #define SCREEN_HEIGHT 224 //Half-height for frame mode (frame buffer will be drawn once for each set of scan lines).
21 
23 
24 #define GIF_PACKET_MAX 4
25 
26 static GS_DRAWENV draw_env[2]; // 2 display environment
27 static GS_DISPENV disp_env[2]; // 2 draw environment
28 
29 static GS_GIF_PACKET packets[2][GIF_PACKET_MAX]; //we need 2 for double buffering
30 static GS_PACKET_TABLE packet_table[2]; //we need 2 for double buffering
31 
32 static int InitGraphics(void);
33 static void SelectDisplayContext(int context_id);
34 static void ClearDrawingContext(int context_id);
35 static int DrawTriangles(GS_PACKET_TABLE *table, int context_index);
36 static void MovePoint(void);
37 
38 static int active_buffer=0; // the buffer that we are currently writiing stuff to
39 
40 int main(int argc, char *argv[])
41 {
42  InitGraphics();
43 
45  packet_table[0].packets = &packets[0][0];
46 
48  packet_table[1].packets = &packets[1][0];
49 
50 /*
51  // change Background color
52 
53  draw_env[0].bg_color.b = 255;
54  draw_env[0].bg_color.r = 255;
55  draw_env[0].bg_color.g = 255;
56 
57  draw_env[1].bg_color.b = 255;
58  draw_env[1].bg_color.r = 255;
59  draw_env[1].bg_color.g = 255;
60 */
61 
62  while(1)
63  {
65 
66  GsGifPacketsClear(&packet_table[active_buffer]); // clear the area that we are going to put the sprites/triangles/....
67 
68  MovePoint();
69  DrawTriangles(&packet_table[active_buffer], active_buffer); //add stuff to the packet area
70 
71  GsDrawSync(0); //wait for the previous buffer to finish drawing
72  GsVSync(0);
73 
74  //display the previous drawn buffer
75  SelectDisplayContext(GsDbGetDisplayBuffer()); //tell CRTC which context we want to see on our tv
76 
77  // clear the draw environment before we draw stuff on it
79 
80  GsGifPacketsExecute(&packet_table[active_buffer], 0); // '0' we dont have to wait because we have 2 buffers (GsDrawSync(0) will do the wait)
82  }
83 
84  return 0;
85 }
86 
87 static int InitGraphics(void)
88 {
89  int env0_address, env1_address;
90 
92 
93  //alloc 2 buffers in vram
96 
97  /*********SETUP CONTEX 1 ENVIRONMENT*************/
99  //Retrieve screen offset parameters.
102  GsSetDefaultDrawEnvAddress(&draw_env[0], env0_address);
103 
105  GsSetDefaultDisplayEnvAddress(&disp_env[0], env1_address);
106 
107  /*********SETUP CONTEX 2 ENVIRONMENT*************/
109  GsSetDefaultDrawEnvAddress(&draw_env[1], env1_address);
110 
112  GsSetDefaultDisplayEnvAddress(&disp_env[1], env0_address);
113 
114  //execute draw/display environment(s) (contex 1)
115  GsPutDrawEnv1 (&draw_env[0]);
117 
118  //execute draw/display environment(s) (contex 2)
119  GsPutDrawEnv2 (&draw_env[1]);
121 
122  //set common primitive-drawing settings (Refer to documentation on PRMODE and PRMODECONT registers).
123  GsOverridePrimAttributes(GS_DISABLE, 0, 0, 0, 0, 0, 0, 0, 0);
124 
125  //set transparency settings for context 1 (Refer to documentation on TEST and TEXA registers).
128 
129  //set transparency settings for context 2 (Refer to documentation on TEST and TEXA registers).
132 
133  return 0;
134 }
135 
136 static void SelectDisplayContext(int context_id)
137 {
138  // the CRTC is used to select which contex we see on our TV/VGA/HDTV
139 
140  if(context_id==0)
142 
143  if(context_id==1)
145 }
146 
147 static void ClearDrawingContext(int context_id)
148 {
149  if(context_id==0)
151  else
153 }
154 
155 static int point_x=100,point_y=100;
156 static int dir_x=0, dir_y=0;
157 
158 static int DrawTriangles(GS_PACKET_TABLE *table, int context_index)
159 {
160  QWORD *p;
161 
162  /* For the GIF packets in this example, the EOP flags are set to 1.
163  Rightfully, it should only be set for only the final packet so that the GIF knows when it can safely switch paths,
164  but to keep things simple, it's set to 1 for every packet.
165 
166  The packets are all in the PACKED format. */
167 
168  // top left
169  //Use the uncached segment, to avoid needing to flush the data cache.
170  p = (QWORD*)(UNCACHED_SEG(GsGifPacketsAlloc(table, 6)));
171 
172  gs_setGIF_TAG(((GS_GIF_TAG *)&p[0]), 5,1,0,0,GS_GIF_PACKED,1,gif_rd_ad);
173  gs_setR_PRIM(((GS_R_PRIM *)&p[1]), GS_PRIM_TRI,0, 0, 0, 1, 0, 0, context_index, 0);
174  gs_setR_RGBAQ(((GS_R_RGBAQ *)&p[2]), 200, 100, 100, 0x80, 0.0f);
175  gs_setR_XYZ2(((GS_R_XYZ *)&p[3]), (ScreenOffsetX+0)<<4, (ScreenOffsetY+0)<<4, 0x00000000);
176  gs_setR_XYZ2(((GS_R_XYZ *)&p[4]), (ScreenOffsetX+200)<<4, (ScreenOffsetY+0)<<4, 0x00000000);
177  gs_setR_XYZ2(((GS_R_XYZ *)&p[5]), (ScreenOffsetX+point_x)<<4, (ScreenOffsetY+point_y)<<4, 0x00000000);
178 
179  //top right
180  //Use the uncached segment, to avoid needing to flush the data cache.
181  p = (QWORD*)(UNCACHED_SEG(GsGifPacketsAlloc(table, 6)));
182 
183  gs_setGIF_TAG(((GS_GIF_TAG *)&p[0]), 5,1,0,0,GS_GIF_PACKED,1,gif_rd_ad);
184  gs_setR_PRIM(((GS_R_PRIM *)&p[1]), GS_PRIM_TRI,0, 0, 0, 1, 0, 0, context_index, 0);
185  gs_setR_RGBAQ(((GS_R_RGBAQ *)&p[2]), 100, 200, 100, 0x80, 0.0f);
186  gs_setR_XYZ2(((GS_R_XYZ *)&p[3]), (ScreenOffsetX+SCREEN_WIDTH-200)<<4, (ScreenOffsetY+0)<<4, 0x00000000);
187  gs_setR_XYZ2(((GS_R_XYZ *)&p[4]), (ScreenOffsetX+SCREEN_WIDTH)<<4, (ScreenOffsetY+0)<<4, 0x00000000);
188  gs_setR_XYZ2(((GS_R_XYZ *)&p[5]), (ScreenOffsetX+point_x)<<4, (ScreenOffsetY+point_y)<<4, 0x00000000);
189 
190  //bottom right
191  //Use the uncached segment, to avoid needing to flush the data cache.
192  p = (QWORD*)(UNCACHED_SEG(GsGifPacketsAlloc(table, 6)));
193 
194  gs_setGIF_TAG(((GS_GIF_TAG *)&p[0]), 5,1,0,0,GS_GIF_PACKED,1,gif_rd_ad);
195  gs_setR_PRIM(((GS_R_PRIM *)&p[1]), GS_PRIM_TRI,0, 0, 0, 1, 0, 0, context_index, 0);
196  gs_setR_RGBAQ(((GS_R_RGBAQ *)&p[2]), 100, 100, 200, 0x80, 0.0f);
197  gs_setR_XYZ2(((GS_R_XYZ *)&p[3]), (ScreenOffsetX+SCREEN_WIDTH-200)<<4, (ScreenOffsetY+SCREEN_HEIGHT)<<4, 0x00000000);
198  gs_setR_XYZ2(((GS_R_XYZ *)&p[4]), (ScreenOffsetX+SCREEN_WIDTH)<<4, (ScreenOffsetY+SCREEN_HEIGHT)<<4, 0x00000000);
199  gs_setR_XYZ2(((GS_R_XYZ *)&p[5]), (ScreenOffsetX+point_x)<<4, (ScreenOffsetY+point_y)<<4, 0x00000000);
200 
201  //bottom left
202  //Use the uncached segment, to avoid needing to flush the data cache.
203  p = (QWORD*)(UNCACHED_SEG(GsGifPacketsAlloc(table, 6)));
204 
205  gs_setGIF_TAG(((GS_GIF_TAG *)&p[0]), 5,1,0,0,GS_GIF_PACKED,1,gif_rd_ad);
206  gs_setR_PRIM(((GS_R_PRIM *)&p[1]), GS_PRIM_TRI,0, 0, 0, 1, 0, 0, context_index, 0);
207  gs_setR_RGBAQ(((GS_R_RGBAQ *)&p[2]), 100, 200, 200, 0x80, 0.0f);
208  gs_setR_XYZ2(((GS_R_XYZ *)&p[3]), (ScreenOffsetX+0)<<4, (ScreenOffsetY+SCREEN_HEIGHT)<<4, 0x00000000);
209  gs_setR_XYZ2(((GS_R_XYZ *)&p[4]), (ScreenOffsetX+200)<<4, (ScreenOffsetY+SCREEN_HEIGHT)<<4, 0x00000000);
210  gs_setR_XYZ2(((GS_R_XYZ *)&p[5]), (ScreenOffsetX+point_x)<<4, (ScreenOffsetY+point_y)<<4, 0x00000000);
211 
212  return 0;
213 }
214 
215 static void MovePoint(void)
216 {
217  if(dir_x==0)point_x -= 6;
218  if(dir_x==1)point_x += 6;
219 
220  if(dir_y==0)point_y -= 8;
221  if(dir_y==1)point_y += 8;
222 
223  if(point_x >SCREEN_WIDTH)
224  {
226  dir_x=0;
227  }
228 
229  if(point_x <0)
230  {
231  point_x= 0;
232  dir_x=1;
233  }
234 
236  {
238  dir_y=0;
239  }
240 
241  if(point_y <0)
242  {
243  point_y= 0;
244  dir_y=1;
245  }
246 }
247 
248 /*EOF*/
int main(int argc, char **argv)
Definition: main.c:350
#define UNCACHED_SEG(x)
Definition: kernel.h:35
static GS_PACKET_TABLE packet_table[2]
Definition: main.c:30
static int InitGraphics(void)
Definition: main.c:87
static int dir_x
Definition: main.c:156
#define SCREEN_WIDTH
Definition: main.c:19
#define GIF_PACKET_MAX
Definition: main.c:24
static GS_DRAWENV draw_env[2]
Definition: main.c:26
#define SCREEN_HEIGHT
Definition: main.c:20
static int point_y
Definition: main.c:155
static int active_buffer
Definition: main.c:38
static int dir_y
Definition: main.c:156
static int ScreenOffsetY
Definition: main.c:22
static GS_GIF_PACKET packets[2][GIF_PACKET_MAX]
Definition: main.c:29
static GS_DISPENV disp_env[2]
Definition: main.c:27
static int ScreenOffsetX
Definition: main.c:22
static void ClearDrawingContext(int context_id)
Definition: main.c:147
static int DrawTriangles(GS_PACKET_TABLE *table, int context_index)
Definition: main.c:158
static void MovePoint(void)
Definition: main.c:215
static void SelectDisplayContext(int context_id)
Definition: main.c:136
static int point_x
Definition: main.c:155
void GsDrawSync(int mode)
Definition: sync.c:18
void GsSetDefaultDrawEnvAddress(GS_DRAWENV *drawenv, u16 vram_addr)
Definition: draw.c:47
void GsSetCRTCSettings(u64 settings, u8 alpha_value)
Definition: libgs.c:56
#define CRTC_SETTINGS_DEFAULT1
Definition: libgs.h:1884
#define GS_ENABLE
Definition: libgs.h:63
QWORD * GsGifPacketsAlloc(GS_PACKET_TABLE *table, u32 num_qwords)
Definition: packets.c:20
void GsPutDisplayEnv1(GS_DISPENV *dispenv)
Definition: draw.c:201
void GsEnableAlphaTransparency2(u16 enable, u16 method, u8 alpha_ref, u16 fail_method)
Definition: primitives.c:94
void GsVSync(int mode)
Definition: sync.c:50
int GsGifPacketsExecute(GS_PACKET_TABLE *table, u16 wait)
Definition: packets.c:76
@ GS_MODE_NTSC
Definition: libgs.h:56
void GsPutDisplayEnv2(GS_DISPENV *dispenv)
Definition: draw.c:238
void GsDbSwapBuffer(void)
Definition: DoubleBuff.c:30
void GsClearDrawEnv2(GS_DRAWENV *drawenv)
Definition: draw.c:219
void GsEnableAlphaBlending2(u16 enable)
Definition: primitives.c:160
#define gs_setR_RGBAQ(p, _r, _g, _b, _a, _q)
Definition: libgs.h:1631
#define gs_setR_XYZ2(p, _x, _y, _z)
Definition: libgs.h:1651
void GsEnableAlphaTransparency1(u16 enable, u16 method, u8 alpha_ref, u16 fail_method)
Definition: primitives.c:72
#define GS_FFMD_FRAME
Definition: libgs.h:43
#define gs_setGIF_TAG(p, _nloop, _eop, _pre, _prim, _flg, _nreg, _reg)
Definition: libgs.h:1870
#define gif_rd_ad
Definition: libgs.h:326
void GsSetDefaultDisplayEnvAddress(GS_DISPENV *dispenv, u16 vram_addr)
Definition: draw.c:164
#define GS_DISABLE
Definition: libgs.h:62
void GsSetDefaultDisplayEnv(GS_DISPENV *dispenv, u16 psm, u16 w, u16 h, u16 dx, u16 dy)
Definition: draw.c:74
void GsGifPacketsClear(GS_PACKET_TABLE *table)
Definition: packets.c:70
void GsPutDrawEnv2(GS_DRAWENV *drawenv)
Definition: draw.c:207
void GsOverridePrimAttributes(s8 override, s8 iip, s8 tme, s8 fge, s8 abe, s8 aa1, s8 fst, s8 ctxt, s8 fix)
Definition: primitives.c:23
int GsDbGetDisplayBuffer(void)
Definition: DoubleBuff.c:25
#define GS_PIXMODE_32
Definition: libgs.h:77
int GsDbGetDrawBuffer(void)
Definition: DoubleBuff.c:20
@ GS_GIF_PACKED
Definition: libgs.h:181
void GsSetDefaultDrawEnv(GS_DRAWENV *drawenv, u16 psm, u16 w, u16 h)
Definition: draw.c:23
#define gs_setR_PRIM(p, _prim_type, _iip, _tme, _fge, _abe, _aa1, _fst, _ctxt, _fix)
Definition: libgs.h:1627
#define CRTC_SETTINGS_DEFAULT2
Definition: libgs.h:1885
void GsEnableAlphaBlending1(u16 enable)
Definition: primitives.c:137
@ GS_ALPHA_GEQUAL
Definition: libgs.h:111
int GsVramAllocFrameBuffer(s16 w, s16 h, s16 psm)
Definition: texture.c:105
void GsPutDrawEnv1(GS_DRAWENV *drawenv)
Definition: draw.c:169
#define GS_INTERLACED
Definition: libgs.h:38
void GsResetGraph(short int mode, short int interlace, short int omode, short int ffmode)
void GsClearDrawEnv1(GS_DRAWENV *drawenv)
Definition: draw.c:182
#define GS_INIT_RESET
Definition: libgs.h:33
@ GS_PRIM_TRI
Definition: libgs.h:70
u16 offset_y
Definition: libgs.h:1944
u16 offset_x
Definition: libgs.h:1942
u32 packet_count
Definition: libgs.h:2289
GS_GIF_PACKET * packets
Definition: libgs.h:2292
Definition: libgs.h:333