• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Gl_Terminator

NEHE TGA C texture loading ported to c#

0 posts in this topic

I ought a lot to NEHE it was my way to start in this world and so I am going to share my C# tga loading code, compressed tga loading its not implemented
this code works

struct TextureImage
{
public byte[] imageData; // Image Data (Up To 32 Bits)
public int bpp; // Image Color Depth In Bits Per Pixel.
public int width; // Image Width
public int height; // Image Height
public int texID; // Texture ID Used To Select A Texture
}


public static bool Compare(byte[] array1, byte[] array2) // this is called Helper.compare
{
int length1 = array1.Length;
int length2 = array2.Length;
if (length1 != length2)
{
return false;
}
// if continue the 2 lenghts are the same
for (int i = 0; i < length1; i++)
{
if (array1[i] != array2[i])
{
return false;
}
}
return true;
}

static int LoadTGA(string filename)// this function return the integer texture id
{
byte[] uTGACompare = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Uncompressed TGA Header
byte[] cTGACompare = { 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Compressed TGA Header
byte[] TGACompare = new byte[12]; // Used To Compare TGA Header

FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read); // Open The TGA File
file.Read(TGACompare, 0, 12);
if (Helper.Compare(TGACompare, uTGACompare))
{
return LoadUncompressedTGA(file, filename);
}
else
{
if (Helper.Compare(TGACompare, uTGACompare))
{
return LoadCompressedTGA(file);
}
}
file.Close();
return 0;
}

/// <summary>
/// This function load an uncompressed TGA
/// </summary>
static int LoadUncompressedTGA(FileStream file, string name)
{
byte[] header = new byte[6]; // First 6 Useful Bytes From The Header
int bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File
int imageSize; // Used To Store The Image Size When Setting Aside Ram
int temp; // Temporary Variable
int type = Gl.GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP)

if (file == null || file.Read(header, 0, 6) != 6)
{
if (file == null)
return -1;
else
{
file.Close();
return -1;
}
}
texture.width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
texture.height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)

if (texture.width <= 0 || texture.height <= 0 || (header[4] != 24 && header[4] != 32)) // Is The TGA 24 or 32 Bit?
{
file.Close();
return -1;
}
texture.bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = texture.bpp / 8; // Divide By 8 To Get The Bytes Per Pixel
imageSize = texture.width * texture.height * bytesPerPixel; // Calculate The Memory Required For The TGA Data
texture.imageData = new byte[imageSize]; // Reserve Memory To Hold The TGA Data
if (imageSize == 0 || file.Read(texture.imageData, 0, imageSize) != imageSize)
{
if (texture.imageData != null)
texture.imageData = null;

file.Close();
return -1;
}
for (int i = 0; i < imageSize; i += bytesPerPixel) // Loop Through The Image Data
{ // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
temp = texture.imageData[i]; // Temporarily Store The Value At Image Data 'i'
texture.imageData[i] = texture.imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
texture.imageData[i + 2] = (byte)temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
}

file.Close();
// Build A Texture From The Data
int[] textureArray = new int[1];
textureArray[0] = texture.texID;
Gl.glGenTextures(1, textureArray); // Generate OpenGL texture IDs

Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureArray[0]); // Bind Our Texture
if (texture.bpp == 24) // Was The TGA 24 Bits
{
type = Gl.GL_RGB; // If So Set The 'type' To GL_RGB
}

//if the texture is intended to be rendered anisotropically
if (ContentManager.IsAnisotropic(name) && maximumAnisotropy != 0)
{
Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnisotropy); // anisotropic Filtered
}

Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR); // Linear Filtered
Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); // Linear Filtered
Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, 4, texture.width, texture.height, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, texture.imageData);


texture.texID = textureArray[0];
return texture.texID;
}
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0