#include "mainengine.h"
#include <SDL/SDL.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "sound_commands.h"
using std::ifstream;
CEMain::CEMain(SDL_Surface * scre, int videox, int videoy, int bitdepth)
{
back = scre;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
back = SDL_SetVideoMode(videox,videoy,bitdepth,SDL_SWSURFACE|SDL_FULLSCREEN|SDL_HWPALETTE);
if ( back == NULL )
{
printf("Unable to set %ix%i video: %s\n", videox, videoy,SDL_GetError());
exit(1);
}
SDL_ShowCursor(0);
LoadMap("data/credits.lmao");
SDL_Delay(1000);
Loop();
}
void CEMain::Loop()
{
int done = 0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
keys = SDL_GetKeyState(NULL);
BackGround();
BlockDraw();
SDL_Flip(back);
}
SDL_FreeSurface(background);
SDL_FreeSurface(back);
MusicOff();
}
SDL_Surface * CEMain::ImageLoad(char * file)
{
SDL_Surface *temp1, *temp2;
temp1 = SDL_LoadBMP(file);
temp2 = SDL_DisplayFormat(temp1);
SDL_FreeSurface(temp1);
return temp2;
}
void CEMain::DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, back, &dest);
}
void CEMain::LoadMap(char * filename)
{
file.open(filename);
char bg[512];
file >> bg;
background = ImageLoad(bg);
file >> bg;
ParseSpriteFile(bg);
file >> bg;
ParseMusicFile(bg);
file >> bg;
ParseMapCoord(bg);
PlayMusic(SFX.ambient);
}
void CEMain::BackGround()
{
DrawIMG(background, 0, 0);
}
void CEMain::ParseSpriteFile(char * filename)
{
printf("Parsing sprite file...\n");
sprfile.open(filename);
for (int i=0; i < MAXSPRITE; i++) { SPRITE.spriteb[i][0] = false; }
int loop = 0;
while(!sprfile.eof())
{
sprfile >> SPRITE.sprites[loop];
printf("Value of SPRITE.sprites[%i] is: %s\n",loop,SPRITE.sprites[loop]);
SPRITE.spriteb[loop][0] = true;
loop++;
}
printf("variable loop in function CEMain::ParseSpriteFile looped %i time(s)...\n", loop);
SPRITE.SPIC = new SpriteP[loop];
for (int i=0; i < loop; i++)
{
SPRITE.SPIC[i].sprite = ImageLoad(SPRITE.sprites[i]);
}
sprfile.close(); }
void CEMain::ParseMusicFile(char * filename)
{
printf("Parsing music file...\n");
muzfile.open(filename);
for (int i = 0; i < 5; i++) { SFX.music[i][0] = false; }
while(true)
{
while(!muzfile.eof())
{
muzfile >> bg;
printf("variable _bg_: %s\n",bg);
if (SFX.music[0][0] == false)
{
muzfile >> bg;
strcpy(SFX.ambient,bg);
printf("Ambient: %s\n",bg);
SFX.music[0][0] = true; break; }
if (SFX.music[1][0] == false)
{
muzfile >> bg;
strcpy(SFX.menup,bg);
printf("Menu Up: %s\n",bg);
SFX.music[1][0] = true;
break;
}
if (SFX.music[2][0] == false)
{
muzfile >> bg;
strcpy(SFX.mendown,bg);
printf("Menu Down: %s\n",bg);
SFX.music[2][0] = true;
break;
}
if (SFX.music[3][0] == false)
{
muzfile >> bg;
strcpy(SFX.mensel,bg);
printf("Menu Select: %s\n",bg);
SFX.music[3][0] = true;
break;
}
if (SFX.music[4][0] == false)
{
muzfile >> bg;
strcpy(SFX.mencan,bg);
printf("Menu Cancel: %s\n",bg);
SFX.music[4][0] = true;
}
break; }
if(SFX.music[4][0] == true)
{
break; }
}
muzfile.close();
printf("Music file parsed...\n");
}
void CEMain::ParseMapCoord(char * filename)
{
coorfile.open(filename);
printf("Starting coordinate parsing...\n");
coorfile >> BLOCK.BlockNum;
BLOCK.blockinfo = new BlockInfo[BLOCK.BlockNum];
for (int i = 0; i < BLOCK.BlockNum; i++)
{
coorfile >> BLOCK.blockinfo[i].tex >> BLOCK.blockinfo[i].x >>
BLOCK.blockinfo[i].y >> BLOCK.blockinfo[i].collision >>
BLOCK.blockinfo[i].w >> BLOCK.blockinfo[i].h;
}
coorfile.close();
printf("Finished coordinate parsing...\n");
}
void CEMain::BlockDraw()
{
for (int i = 0; i < BLOCK.BlockNum; i++)
{
DrawIMG(SPRITE.SPIC[i].sprite, BLOCK.blockinfo[i].x, BLOCK.blockinfo[i].y);
}
}