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

Author Topic: Displaying a sprite  (Read 1742 times)

0 Members and 1 Guest are viewing this topic.

MatsVed

  • Newbie
  • *
  • Posts: 7
    • MSN Messenger - afr088@hotmail.com
    • View Profile
Displaying a sprite
« on: May 27, 2011, 03:07:02 pm »
I'm trying to display a sprite, but it isn't displaying! :(
Could someone try to see what's wrong with my code?

Code: [Select]
// TSOClient.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "TSOClient.h"
#include "UIScene.h" //Required by SceneManager.h in order to compile.
#include "SceneManager.h"
#include "luabind/luabind.hpp"
#include "LuaInterfaceManager.h"
#include "SFML/graphics.hpp"

#define MAX_LOADSTRING 100

class UIBackground;

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

SceneManager SceneMgr;
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

  // TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TSOCLIENT, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

//Initialize Lua...
LuaInterfaceManager LuaMgr;

//Registers all appropriate classes to Lua.
LuaInterfaceManager::RegisterClasses();

//Makes the SceneManager object available to all Lua scripts.
luabind::globals(LuaInterfaceManager::LuaManager)["SceneMgr"] = SceneMgr;

SceneMgr.LoadInitialScreen("gamedata\\luascripts\\personselection.lua");

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TSOCLIENT));

std::vector<UIScene> *ScenePtr = SceneMgr.Scenes();

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Clear the screen (fill it with black color)
        App.Clear();

for(int i = 0; i < ScenePtr->size(); i++)
{
for(int j = 0; j < (*ScenePtr)[i].Backgrounds()->size(); j++)
{
std::vector<UIBackground> *BackgroundsPtr = (*ScenePtr)[i].Backgrounds();

App.Draw((*BackgroundsPtr)[j].GetSprite());
delete BackgroundsPtr;
}
}

        // Display window contents on screen
        App.Display();
}

return (int) msg.wParam;
}


UIScene.cpp:

Code: [Select]
#include "stdafx.h"
#include "UIScene.h"
#include <ezlogger/ezlogger_headers.hpp>

UIScene::UIScene(string ArchivePath)
{
m_ArchivePath = ArchivePath;
m_Archive = FileArchive(m_ArchivePath);
}

void UIScene::LoadBackground(int ID)
{
unsigned char *Destination;
sf::Image Img;

/*EZLOGGERSTREAM << "Attempting to call FARExtractItemFromFileByID()!" << std::endl;
unsigned int NumBytes = FARExtractItemFromFileByID((const char *)m_ArchivePath.c_str(), (unsigned int)ID, &Destination, 0);
EZLOGGERSTREAM << "Called FARExtractItemFromFileByID()!";
Img.LoadFromMemory((const char*)Destination, NumBytes);*/

if(!Img.LoadFromMemory(m_Archive.GetItemByID(ID), m_Archive.ItemDecompressedSize(ID)))
{
EZLOGGERSTREAM << "Couldn't load image from memory!" << std::endl;
}

UIBackground Background(Img);
m_Backgrounds.push_back(Img);
}


SceneManager.cpp:

Code: [Select]
#include "stdafx.h"
#include "SceneManager.h"
#include "LuaInterfaceManager.h"
#include <ezlogger/ezlogger_headers.hpp>

void SceneManager::LoadInitialScreen(std::string Path)
{
if(luaL_dofile(LuaInterfaceManager::LuaManager, Path.c_str()) != 0)
{
//TODO: Log error...
EZLOGGERSTREAM << "Error encountered in Lua script: " << std::string(lua_tostring(LuaInterfaceManager::LuaManager, -1)) << std::endl;
}
else
{
EZLOGGERSTREAM << "Ran Lua script: " << Path << std::endl;
}
}

void SceneManager::AddScreen(UIScene Screen)
{
m_Scenes.push_back(Screen);
}


FileArchive.cpp:

Code: [Select]
#include "stdafx.h"
#include "FileArchive.h"
#include <assert.h>
#include <iostream>
#include <ezlogger/ezlogger_headers.hpp>

string Replace(string Str, string From, string To);

FileArchive::FileArchive()
{
}

FileArchive::FileArchive(string Path)
{
char Signature[8];
char Version[4];
char ManifestOffset[4];
char NumFiles[4];

m_ArchivePath = Path;

ifstream Archive(Path.c_str(), ios::in | ios::binary);
Archive.read(Signature, sizeof(FARSignature));

string StrSignature(Signature, Signature + 8);

assert(StrSignature == "FAR!byAZ");
cout << "Signature: " << StrSignature << "\r\n";

Archive.read(Version, 4);
m_Version = *((int*)Version);

assert(m_Version == 1 || 3);

Archive.read(ManifestOffset, 4);
m_ManifestOffset = *((unsigned int*)ManifestOffset);

Archive.seekg(m_ManifestOffset, ios::beg);

Archive.read(NumFiles, 4);
m_NumFiles = *((unsigned int*)NumFiles);

Archive.close();

//Heap error...
/*delete[] Signature;
delete[] Version;
delete[] ManifestOffset;
delete[] NumFiles;*/

FileArchive::FetchEntries();
}

//Fetches information about an archive's entries.
void FileArchive::FetchEntries()
{
ifstream Archive(m_ArchivePath.c_str(), ios::in | ios::binary);

char DecompressedDataSize[4];
char CompressedDataSize[3];
char DataType[1];
char DataOffset[4];
char Compressed[1];
char AccessNumber[1];
char FilenameLength[2];
char TypeID[4];
char FileID[4];
char* Filename;

//Seek past the number of files...
Archive.seekg(m_ManifestOffset + 4, ios::beg);

for(unsigned int i = 0; i < m_NumFiles; i++)
{
FAR3Entry Entry;

Archive.read(DecompressedDataSize, sizeof(unsigned int));
Entry.DecompressedDataSize = *((unsigned int*)DecompressedDataSize);

Archive.read(CompressedDataSize, 3);
Entry.CompressedDataSize = *((unsigned int*)CompressedDataSize);

Archive.read(DataType, sizeof(unsigned char));
Entry.DataType = *((unsigned char*)DataType);

Archive.read(DataOffset, sizeof(unsigned int));
Entry.DataOffset = *((unsigned int*)DataOffset);

Archive.read(Compressed, sizeof(unsigned char));
Entry.Compressed = *((unsigned char*)Compressed);

Archive.read(AccessNumber, sizeof(unsigned char));
Entry.AccessNumber = *((unsigned char*)AccessNumber);

Archive.read(FilenameLength, sizeof(unsigned short));
Entry.FilenameLength = *((unsigned short*)FilenameLength);

Archive.read(TypeID, sizeof(unsigned int));
Entry.TypeID = *((unsigned int*)TypeID);

Archive.read(FileID, sizeof(unsigned int));
Entry.FileID = *((unsigned int*)FileID);

Filename = new char[Entry.FilenameLength];
Archive.read(Filename, Entry.FilenameLength);
memcpy(&Entry.Filename, Filename, Entry.FilenameLength);

string StrFilename(Filename, Filename + Entry.FilenameLength);
cout << "Filename: " << StrFilename << "\r\n";

m_Entries.push_back(Entry);
//delete &Entry; //Is this a good idea?
}

Archive.close();
}

char* FileArchive::GetItemByID(int ID)
{
ifstream Archive(m_ArchivePath.c_str(), ios::in | ios::binary);

for(int i = 0; i < m_Entries.size(); i++)
{
if(ID == m_Entries[i].FileID)
{
if(!m_Entries[i].Compressed)
{
char* Buffer = new char[m_Entries[i].DecompressedDataSize];

//9 bytes for the RefPak header...
Archive.seekg(m_Entries[i].DataOffset + 9, ios::beg);
Archive.read(Buffer, m_Entries[i].DecompressedDataSize);
EZLOGGERSTREAM << "Read: " << m_Entries[i].DecompressedDataSize << " bytes!" << std::endl;

Archive.close();

return Buffer;
}
else
{
char* CompBuffer = new char[m_Entries[i].CompressedDataSize];
char* DecompBuffer = new char[m_Entries[i].DecompressedDataSize];

Archive.seekg(m_Entries[i].DataOffset, ios::beg);
Archive.read(CompBuffer, m_Entries[i].CompressedDataSize);

if(!decompress((const byte*)CompBuffer, m_Entries[i].CompressedDataSize, (byte*)DecompBuffer,
m_Entries[i].DecompressedDataSize, true))
{
EZLOGGERSTREAM << "Decompression failed!" << std::endl;
}

delete CompBuffer;
Archive.close();

return DecompBuffer;
}
}
}
}

int FileArchive::ItemDecompressedSize(int ID)
{
for(int i = 0; i < m_Entries.size(); i++)
{
if(ID == m_Entries[i].FileID)
return m_Entries[i].DecompressedDataSize;
}
}

string Replace(string Str, string From, string To)
{
int Position = Str.find(From);

while(Position != string::npos)
{
Str = Str.replace(Position, From.length(), To);
Position = Str.find(From, Position + From.length());
}

return Str;
}


UIBackground.h:

Code: [Select]
#ifndef __UIBACKGROUND_H__
#define __UIBACKGROUND_H__

#include <SFML/Graphics.hpp>

class UIBackground
{
private:
sf::Image m_Image;
sf::Sprite m_Sprite;
public:
UIBackground(sf::Image Img) { m_Image = Img; m_Sprite.SetImage(m_Image); };
sf::Sprite GetSprite() { return m_Sprite; };
};

#endif


I've checked that Image.LoadFromMemory returns true, so in theory the sprite should be displaying, but it isn't. :(

Any suggestions?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Displaying a sprite
« Reply #1 on: May 27, 2011, 03:15:43 pm »
Your code is too long. If you have a problem to display a sprite, please show us a code that displays a sprite (and nothing else) -- otherwise we'll lose hours trying to figure out irrelevant code.

Chances are that you find the problem yourself while reducing the code.

I didn't read all your code but I have the feeling that you should read the last part of the sprite tutorial (about copying images).
Laurent Gomila - SFML developer

MatsVed

  • Newbie
  • *
  • Posts: 7
    • MSN Messenger - afr088@hotmail.com
    • View Profile
Displaying a sprite
« Reply #2 on: May 27, 2011, 03:17:17 pm »
The code that displays the sprite is at the top...

Code: [Select]
  //Initialize Lua...
   LuaInterfaceManager LuaMgr;
   
   //Registers all appropriate classes to Lua.
   LuaInterfaceManager::RegisterClasses();

   //Makes the SceneManager object available to all Lua scripts.
   luabind::globals(LuaInterfaceManager::LuaManager)["SceneMgr"] = SceneMgr;

   SceneMgr.LoadInitialScreen("gamedata\\luascripts\\personselection.lua");

   hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TSOCLIENT));

   std::vector<UIScene> *ScenePtr = SceneMgr.Scenes();

   // Main message loop:
   while (GetMessage(&msg, NULL, 0, 0))
   {
      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
      {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }

      // Clear the screen (fill it with black color)
        App.Clear();

      for(int i = 0; i < ScenePtr->size(); i++)
      {
         for(int j = 0; j < (*ScenePtr)[i].Backgrounds()->size(); j++)
         {
            std::vector<UIBackground> *BackgroundsPtr = (*ScenePtr)[i].Backgrounds();

            App.Draw((*BackgroundsPtr)[j].GetSprite());
            delete BackgroundsPtr;
         }
      }

        // Display window contents on screen
        App.Display();
   }

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Displaying a sprite
« Reply #3 on: May 27, 2011, 03:21:51 pm »
Ok but then a lot of parts are missing (where are your sprites/images loaded? how are they stored? ...).

You must provide a complete and minimal source code. Trying to solve this directly within your initial source code is tedious, especially for us. Please help and write a new, minimal and complete source code that reproduces the problem.

By the way, I edited my previous answer (not sure you saw it).
Laurent Gomila - SFML developer