Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Massive Amounts of Images  (Read 2258 times)

0 Members and 1 Guest are viewing this topic.

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Massive Amounts of Images
« on: May 15, 2008, 04:53:41 am »
I'm making a Jeopardy for class, so i have over 36 images (technically less due to reuse of the money images but their variables have to be different) to handle in one application...lots of images and i have a few questions

without copy and pasting and modifying the same code multiple times, how would i detect the mouse pos. to be inside one of the images and go to the right image?

Code: [Select]
int Jeopardy[5][5];

void JeopardyArray()
{
for (int column = 1; column < 5; column++)
{
for (int row = 0; row < 5; row++)
{
Jeopardy[column][row] = 1;
};
};
}
int Question = 0;
int x = 0;
int y = 0;

int main()
{
JeopardyArray();

sf::Image Who;
sf::Image Dates;
sf::Image Where;
sf::Image Numbers;
sf::Image Spelling;
sf::Image Random;

sf::Image n100;
sf::Image n200;
sf::Image n300;
sf::Image n400;
sf::Image n500;

sf::Image HorizBar;

sf::Image VertBar;

sf::Image HorizSep;

sf::RenderWindow Window(sf::VideoMode(1024, 768, 32), "Jeopardy!", sf::Style::Fullscreen, 4);
sf::Color Background(0,0,102);
Window.SetBackgroundColor(Background);

if (!Who.LoadFromFile("G:/Jeopardy/Jeopardy Who.bmp"))
{
return -3;
};

if (!Dates.LoadFromFile("G:/Jeopardy/Jeopardy Dates.bmp"))
{
return -3;
};

if (!Where.LoadFromFile("G:/Jeopardy/Jeopardy Locations.bmp"))
{
return -3;
};

if (!Numbers.LoadFromFile("G:/Jeopardy/Jeopardy Numbers.bmp"))
{
return -3;
};

if (!Spelling.LoadFromFile("G:/Jeopardy/Jeopardy Spelling.bmp"))
{
return -3;
};

if (!Random.LoadFromFile("G:/Jeopardy/Jeopardy Random.bmp"))
{
return -3;
};

if (!n100.LoadFromFile("G:/Jeopardy/$100.bmp"))
{
return -3;
};

if (!n200.LoadFromFile("G:/Jeopardy/$200.bmp"))
{
return -3;
};

if (!n300.LoadFromFile("G:/Jeopardy/$300.bmp"))
{
return -3;
};

if (!n400.LoadFromFile("G:/Jeopardy/$400.bmp"))
{
return -3;
};

if (!n500.LoadFromFile("G:/Jeopardy/$500.bmp"))
{
return -3;
};

sf::Sprite Category1(Who);
sf::Sprite Category2(Dates);
sf::Sprite Category3(Where);
sf::Sprite Category4(Numbers);
sf::Sprite Category5(Spelling);
sf::Sprite Category6(Random);

sf::Sprite r11(n100);
sf::Sprite r21(n200);
sf::Sprite r31(n300);
sf::Sprite r41(n400);
sf::Sprite r51(n500);

sf::Sprite r12(n100);
sf::Sprite r22(n200);
sf::Sprite r32(n300);
sf::Sprite r42(n400);
sf::Sprite r52(n500);

sf::Sprite r13(n100);
sf::Sprite r23(n200);
sf::Sprite r33(n300);
sf::Sprite r43(n400);
sf::Sprite r53(n500);

sf::Sprite r14(n100);
sf::Sprite r24(n200);
sf::Sprite r34(n300);
sf::Sprite r44(n400);
sf::Sprite r54(n500);

sf::Sprite r15(n100);
sf::Sprite r25(n200);
sf::Sprite r35(n300);
sf::Sprite r45(n400);
sf::Sprite r55(n500);

sf::Sprite r16(n100);
sf::Sprite r26(n200);
sf::Sprite r36(n300);
sf::Sprite r46(n400);
sf::Sprite r56(n500);

Category2.SetPosition(173,0);
Category3.SetPosition(346,0);
Category4.SetPosition(519,0);
Category5.SetPosition(692,0);
Category6.SetPosition(865,0);

r11.SetPosition(0, 141);
r21.SetPosition(0, 265);
r31.SetPosition(0, 389);
r41.SetPosition(0, 513);
r51.SetPosition(0, 637);

r12.SetPosition(171, 141);
r22.SetPosition(171, 265);
r32.SetPosition(171, 389);
r42.SetPosition(171, 513);
r52.SetPosition(171, 637);

r13.SetPosition(344, 141);
r23.SetPosition(344, 265);
r33.SetPosition(344, 389);
r43.SetPosition(344, 513);
r53.SetPosition(344, 637);

r14.SetPosition(517, 141);
r24.SetPosition(517, 265);
r34.SetPosition(517, 389);
r44.SetPosition(517, 513);
r54.SetPosition(517, 637);

r15.SetPosition(690, 141);
r25.SetPosition(690, 265);
r35.SetPosition(690, 389);
r45.SetPosition(690, 513);
r55.SetPosition(690, 637);

r16.SetPosition(863, 141);
r26.SetPosition(863, 265);
r36.SetPosition(863, 389);
r46.SetPosition(863, 513);
r56.SetPosition(863, 637);

sf::Music Music;

if (!Music.OpenFromFile("G:/Jeopardy/Jeopardy.wav"))
{
return -2;
};
Music.Play();


if (!HorizBar.LoadFromFile("G:/Jeopardy/Jeopardy Horiz Bar.bmp"))
{
return -3;
};

sf::Sprite HBar(HorizBar);
HBar.SetPosition(0, 124);


if (!VertBar.LoadFromFile("G:/Jeopardy/Jeopardy Verti Bar.bmp"))
{
return -3;
};

sf::Sprite VBar1(VertBar);
sf::Sprite VBar2(VertBar);
sf::Sprite VBar3(VertBar);
sf::Sprite VBar4(VertBar);
sf::Sprite VBar5(VertBar);
VBar1.SetPosition(159,0);
VBar2.SetPosition(332,0);
VBar3.SetPosition(505,0);
VBar4.SetPosition(678,0);
VBar5.SetPosition(851,0);


if (!HorizSep.LoadFromFile("G:/Jeopardy/Jeopardy Horiz Sep.bmp"))
{
return -3;
};

sf::Sprite HSep1(HorizSep);
sf::Sprite HSep2(HorizSep);
sf::Sprite HSep3(HorizSep);
sf::Sprite HSep4(HorizSep);
HSep1.SetPosition(0,260);
HSep2.SetPosition(0,384);
HSep3.SetPosition(0,508);
HSep4.SetPosition(0,632);

sf::Image VertSep;
if (!VertSep.LoadFromFile("G:/Jeopardy/Jeopardy Sep Bar.bmp"))
{
return -3;
};
sf::Sprite VSep1(VertSep);
sf::Sprite VSep2(VertSep);
sf::Sprite VSep3(VertSep);
sf::Sprite VSep4(VertSep);
sf::Sprite VSep5(VertSep);
VSep1.SetPosition(162,0);
VSep2.SetPosition(335,0);
VSep3.SetPosition(508,0);
VSep4.SetPosition(681,0);
VSep5.SetPosition(854,0);

sf::Image JT;
if (!JT.LoadFromFile("G:/Jeopardy/JTEST.bmp"))
{
return -3;
};
sf::Sprite JTEST(JT);

bool Running = true;
while(Running)
    {
sf::Event Event;
const sf::Input& Input = Window.GetInput();
int MouseX = Input.GetMouseX();
int MouseY = Input.GetMouseY();
       
while (Window.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                Running = false;
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Running = false;
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F))
Music.Play();

if ((0 < MouseX && MouseX < 162 && 124 < MouseY && MouseY < 260) && (Event.MouseButton.Button == sf::Mouse::Left) && (Jeopardy[1][1] == 1))
{
Question = 1;
y = 1;
x = 1;

};

if ((0 < MouseX && MouseX < 162 && 124 < MouseY && MouseY < 260) && (Event.MouseButton.Button == sf::Mouse::Left) && (Jeopardy[2][1] == 1))
{
Question = 2;
y = 2;
x = 2;

};

if ((0 < MouseX && MouseX < 162 && 124 < MouseY && MouseY < 260) && (Event.MouseButton.Button == sf::Mouse::Left) && (Jeopardy[3][1] == 1))
{
Question = 3;
y = 3;
x = 3;

};

if ((0 < MouseX && MouseX < 162 && 124 < MouseY && MouseY < 260) && (Event.MouseButton.Button == sf::Mouse::Left) && (Jeopardy[4][1] == 1))
{
Question = 4;
y = 4;
x = 4;

};

if ((0 < MouseX && MouseX < 162 && 124 < MouseY && MouseY < 260) && (Event.MouseButton.Button == sf::Mouse::Left) && (Jeopardy[5][1] == 1))
{
Question = 5;
y = 5;
x = 5;

};

if ((0 < MouseX && MouseX < 162 && 124 < MouseY && MouseY < 260) && (Event.MouseButton.Button == sf::Mouse::Left) && (Jeopardy[6][1] == 1))
{
Question = 6;
y = 6;
x = 6;

};

if ((Event.MouseButton.Button == sf::Mouse::Right) && (Question > 0))
{
Jeopardy[y][x] = 0;
Question = 0;
};
        };
       
if (Jeopardy[1][1] == 1)
Window.Draw(r11);
if (Jeopardy[2][1] == 1)
Window.Draw(r21);
if (Jeopardy[3][1] == 1)
Window.Draw(r31);
if (Jeopardy[4][1] == 1)
Window.Draw(r41);
if (Jeopardy[5][1] == 1)
Window.Draw(r51);

Window.Draw(r12);
Window.Draw(r22);
Window.Draw(r32);
Window.Draw(r42);
Window.Draw(r52);

Window.Draw(r13);
Window.Draw(r23);
Window.Draw(r33);
Window.Draw(r43);
Window.Draw(r53);

Window.Draw(r14);
Window.Draw(r24);
Window.Draw(r34);
Window.Draw(r44);
Window.Draw(r54);

Window.Draw(r15);
Window.Draw(r25);
Window.Draw(r35);
Window.Draw(r45);
Window.Draw(r55);

Window.Draw(r16);
Window.Draw(r26);
Window.Draw(r36);
Window.Draw(r46);
Window.Draw(r56);

Window.Draw(Category1);
Window.Draw(Category2);
Window.Draw(Category3);
Window.Draw(Category4);
Window.Draw(Category5);
Window.Draw(Category6);
Window.Draw(HBar);
Window.Draw(HSep1);
Window.Draw(HSep2);
Window.Draw(HSep3);
Window.Draw(HSep4);
Window.Draw(VBar1);
Window.Draw(VBar2);
Window.Draw(VBar3);
Window.Draw(VBar4);
Window.Draw(VBar5);
Window.Draw(VSep1);
Window.Draw(VSep2);
Window.Draw(VSep3);
Window.Draw(VSep4);
Window.Draw(VSep5);
if (Question == 1)
Window.Draw(JTEST);

Window.Display();
    }
    return EXIT_SUCCESS;


//THIS CODE IS NOT FINISHED!

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Re: Massive Amounts of Images
« Reply #1 on: May 15, 2008, 07:17:39 am »
I didn't look at the code (it's very big, uncommented, etc.) but if you want to find what "cell" a mouse click is on, just divide the mouse coordinates by the cell sizes.
Also, there is no need to have multiple variables for the same image.  Multiple sprites can use the same image variable.  You should probably be using image pointers to move them around easiest.
If you have a specific question, ask it and post the relevant code.  Please don't post your entire project since it forces anyone trying to help you to hunt for the relevant part.

Edit:  if you're typing basically the same code over and over, you're probably doing something wrong.  You should be using a loop, function calls, or something.  There's no reason to have 30 lines each drawing a sprite.  Put all your sprites in an STL vector (or an array) and iterate through them.