• Content count

  • Joined

  • Last visited

Community Reputation

160 Neutral

About MrFraggs

  • Rank
  1. Advice With Game Loop

    Alright i'll have to try both options out and see how they work, preferably across multiple pieces of hardware. Thanks for all the help, especially Ectara! It's hard to believe some random guy across the interwebs appreciating much, but believe me, I do :)
  2. Advice With Game Loop

      Running at 4000+ FPS is definitely not a goal, just something it's doing. I'm not limiting it for now, just because I don't really have a need to aside from less cycles. Once I start adding things to the game, i'll see how they effect the FPS, and if it's capped, I won't know how it effects it unless it drops below my target. Then, when i'm confident that I won't be adding anything else that may significantly lower my FPS, I can work on capping it. My logic isn't currently based on FPS, but the amount of Update() passes per second, which I have set to 60 right now.   Also, I believe using any kind of Sleep() function when not explicitly programming multiple cores is bad. That's just what i've picked up over the years of learning, so until I get into that area if need be, I want to stay away from anything that puts the thread to sleep for now. I appreciate the suggestions though, definitely gives me things to think about!   Now, Ectara, I haven't really implemented any kind of time based logic, and I think that using a semi-fixed time step loop like this one, I read that it isn't necessary all the time. I could be way off, as I've only really spent the last few days deeply researching this. I came from XNA, which basically handled all this stuff for me. I did use ElapsedGameTime in XNA, which I think I can easily use here. That time function I used should keep track of the total time the game has been running, and if not, I can always use the less-precise SDL_GetTicks(). But say I was moving a sprite, and had something like this. This obviously wouldn't be all the logic, but I think it gets my point across:   SpritePositionX = 0 SpriteSpeed = 5   So, since I tried to get the updates to go the same amount of time (hence semi-fixed step, which is how described it), each update if I were to move, I would do:   SpritePositionX = SpritePositionX + SpriteSpeed.   I know with game time based updating (which i'm not saying i WON'T use), since i'm trying to get Update() to run so many times per second, wouldn't it move 5 pixels each pass, which would be 60 x 5 pixels per second? I tried passing both dt and t to my Update method, and when playing around with variables increasing by 1 * some game time, I couldn't find a difference in increasing it by a static amount, like I used with SpriteSpeed, and increasing it by say, SpriteSpeed * gameTime. I understand the concept, but I can't really figure out if implementing it in my current design is necessary.   I could definitely be wrong on all of this, down to the entire game loop itself. Definitely not saying i'm right at all on anything, which is why i'm coming to you guys for help! Should I go by game time, which I think would look something like this:   Update(gameTime) {    GetLastGameTime;    GetCurrentGameTime;   Find the difference;    Calculate logic based on difference; }   I'm assuming based on your comments above that the game loop I have now will work well, (I hope!), so I think any changes I make come down to whether to update my logic based on time or Update() passes. 
  3. Advice With Game Loop

    Ectara, I see what you meant now, sorry if it sounded like I came off as arrogant on that one! I didn't mean to at all, I just thought that what I did wasn't really clear, since you don't have all my code in front of you, just snippets.   Thanks for the replies, I went ahead and spent a day working through the link you guys posted trying to understand it better than I did before. I found a silly mistake that caused those updates and render cycle numbers to be off. The timer's he uses is based on seconds, even though they aren't real timers. They clearly say, "hi_res_timers_in_seconds", and no matter how many times I read it, nothing clicked. I finally saw it about two or three hours ago and got it fixed. Now my Update() runs properly at about 60 times per second, or whatever I set it to!    I only have two more questions, and i'll post my current loop below. First, will this loop run the update function on most computers the same amount of times? I know if a computer can't handle it, the frame rate will drop, which is expected. But will the game logic run at the same speed even when the frames drop? I probably sound like a broken record, and for a 2D game it probably won't even be a huge problem, but if i'm going to learn something, I want to learn it right :P   Second, i'm not sure if this is based on my UPS calculations, or the game logic itself, but my UPS runs between 59.99xxx and 60.00xxx, where the xxx are any numbers, usually the same each time it switches. Is this really a huge problem, or can I ignore that?  It switches between the two constantly, and i'm not sure if that is going to effect my game logic. I set up a test where each update loop i have an x variable that increases by 1 each pass, so I could see how many times per second it updates. In theory, it should update by 60 each time if i'm running the update loop 60 times per second. It does a pretty good job of that, but every few seconds it will increase by 61 or so, meaning sometimes Update() will run an extra time. Will this end up being a huge problem, or is it normal and I should just ignore it?    Thanks for all the help, I really appreciate the time and effort you guys are putting in to assist! :)   Here's my code, I grabbed the timer function online somewhere, as I was having some trouble grasping the QueryPerformanceCounter() function.   Main Loop: const float dt = 1.0f / 60.0f; //60 Would be desired updates per second float currentTime = game->Time(); //Get the current time. float accumulator = 0.0f; while(game->IsRunning()) { while(SDL_PollEvent(&e)) { //Exit the game if the user hits the 'X' button on the window. if(e.type == SDL_QUIT) game->SetIsRunning(false); } //Game update, render loop float newTime = game->Time(); float frameTime = (newTime - currentTime); //Convert to seconds if(frameTime > 0.25f) frameTime = 0.25f; currentTime = newTime; accumulator += frameTime; while(accumulator >= dt) { game->Update(); accumulator -= dt; } game->Render(); } // End main loop Timer Function: //hi res timer in seconds float Game::Time() { static __int64 start = 0; static __int64 frequency = 0; if (start==0) { QueryPerformanceCounter((LARGE_INTEGER*)&start); QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); return 0.0f; } __int64 counter = 0; QueryPerformanceCounter((LARGE_INTEGER*)&counter); return (float) ((counter - start) / float(frequency)); } UPS/FPS Tracking: updateCount++; time = Time(); UPS = updateCount / (time - timeBase); FPS = frameCount / (time - timeBase); //Set the title each second if(time - timeBase >= 1) { timeBase = time; updateCount = 0.0f; frameCount = 0.0f; std::stringstream strs; strs << "FPS: "<< FPS << " UPS: " << UPS; std::string temp = strs.str(); char* theTitle = (char*)temp.c_str(); //Set window title to fps SDL_SetWindowTitle(mWindow, theTitle); }
  4. Advice With Game Loop

    So I reworked my loop, and this is what I have, but with an unexpected result: double t = 0.0; const double dt = 0.01; double currentTime = SDL_GetTicks(); double accumulator = 0.0; while(game->IsRunning()) { while(SDL_PollEvent(&e)) { //Exit the game if the user hits the 'X' button on the window. if(e.type == SDL_QUIT) game->SetIsRunning(false); } //Game update, render loop double newTime = SDL_GetTicks(); double frameTime = newTime - currentTime; if(frameTime > 0.25) frameTime = 0.25; currentTime = newTime; accumulator += frameTime; while(accumulator >= dt) { game->Update(); accumulator -= dt; t += dt; } game->Render(); } // End main loop Now, I know that SDL_GetTicks() only has a resolution of 1ms, so I may find a better one soon. Here is my code for calculating frames per second and updates per second: updateCount++; time = SDL_GetTicks(); if(time - timeBase > 1000) { UPS = updateCount * 1000.0 / (time - timeBase); FPS = frameCount * 1000.0 / (time - timeBase); timeBase = time; updateCount = 0; frameCount = 0; } timeBase, frameCount and updateCount are all initialized to zero in the constructor, and frameCount++ is located in the render function, but I didn't find it necessary to include that entire function for one line of code, which is basically in Update() as well.    Now, here is where I'm confused. the FPS is about 270, while the UPS is about 7000. This seems a little off, but I could be wrong. I haven't incorporated the time stuff into the Update() function yet, since there's nothing on the screen to worry about updating, but I will when I get this loop going correct. How does it look to you guys? Would you do anything different? Thanks!
  5. Advice With Game Loop

    Like I said, that has nothing to do with the actual FPS/UPS calculations. The FPS/UPS variable it updates to is STRICTLY for display purposes. I added that code in after I had the rates updating at the same time as it had after I added that code. It has NOTHING to do with the actual fps/ups calculations, just the display of information that tells me how many times per second the game is updating. If I run the game, which is just a blue screen still, it updates the title of the window with that information, telling me that i'm getting about 4000-4500 FPS, and about 60-62 UPS right now. But instead of that number in the title bar changing 60 times per second, which is how many times Update() get's called per second (UPS), it checks to see if a second in time has passed, and if so, updates the title bar. Nothing more, nothing less. I can't stress that enough. It has absolutely NOTHING to do with the actual game loop.   I used Pokemon as more of an example of the display I was going for, with a kind of top down approach with a full 2D world, and not a 2D side scroller. I have no intentions of emulating Pokemon gameplay at all, but at the time, that was the first game that popped into my head to use as an example of the type of display I was looking for. I should have said, not a 2D side scroller, but a 2D tile based world.    GafferonGames' link is the only other link I used that I actually tried to implement the gameplay, but it almost seemed a little too complicated for a smaller indie title. Not only that, but I was having trouble trying to decipher the actual game loop in terms of FPS/UPS. Going for a time based Update() is my goal here, I guess i'm just implementing it wrong. I'll have to look over it again, for a third day in a row, and see if I can grasp it any better than I previously had. One question about this article though, is the integrate() function basically the Update() function i'm trying to use? Keep in mind, my game logic hasn't really been born yet, which is why I don't even pass a gameTime variable into Update(). It will eventually, but like I said, I need to get the main game loop running in a way I'm happy with and know it SHOULD run about the same on MOST systems first.
  6. Advice With Game Loop

    I'm curious if this part does what I think it does. Do you really mean to check to see if the time taken to update before drawing takes an even multiple of 1000ms, or am I missing something?   The most important thing is what kind of game/application will this be? This might work well for a little side-scroller, but for a bitmap editor, it is a horrible way to do things. For the most part, you absorbed the lesson, but truly, the loop's purpose determines its structure.   All that part does is update the FPS and UPS every second, instead of 60 times per second. No need to see the numbers change at a speed when you can't even read them! I think the only thing I need to worry about is this program running the update cycle pretty much at the same speed on all computers. I believe the point of the tutorial I followed was to do just that, and if the user's computer is slow, to shave off some frame rate instead of losing game logic. That was my ultimate goal with my game loop, so I was hoping to have someone tell me whether I actually implemented it right or not.   But yeah, the code you questioned just keeps track of the FPS/UPS and updates it once per second instead of 60, since the game updates 60 times per second. The game is going to be a tile based 2D game like I said, but if you need a visual on exactly how the gameplay might look, think of like Hotline Miami, or even Pokemon games, the way it's a big map where you can move along both x and y planes. I want to use the term top down, but loosely. 
  7. Advice With Game Loop

    I planned on doing it based off time, I just haven't got far enough into development to get that far. My main goal now was just setting up the skeleton of the game, which started with getting the frame rate and update rate running at the right rate.  What information might you need? There isn't a whole lot more to my game right now aside from initializing SDL, and some empty functions that haven't been put to use yet. Right now, Update() just set's the title of the window to display the FPS/UPS, as I haven't gotten far enough to implement my own bitmap font.    My main goal right now is just to be able to get a steady game loop going. What would you recommend I do with it? I was using this:   The third example, modified a few variable names and added code to keep track of FPS/UPS. Let me know what I can do to help you help me! :)
  8. Hey everyone, how goes it?   So i've been working on programming a game in C++/SDL2.0 ever since SDL 2.0 release candidate came out. I have spent about 2 days straight of doing nothing except trying to find and implement a game loop I thought would be best for me. Now, I don't mean best ever, I just mean deciding between fixed step and variable step, etc. Basically what I have now is a game loop that calls the Update() function about 60 times per second, while not capping the framerate. I have been thinking about also capping the frame rate to 60, but i'm not sure if that's necessary yet, as all I draw on the screen for now is a blue color. The Update() function will update all of the game logic when it starts to be implemented, while Render() draws everything. Ever since I switched back to C++ from XNA, i've been trying to basically emulate a XNA framework into C++, as that's where I found it to be the easiest.    If it matters at all, i'm planning on making a 2D tile based game. So do you guys mind taking a look at my main game loop, and telling me if I should change it over to a different setup? Here is the code to just the main game loop: //Event structure SDL_Event e; const int UPDATES_PER_SECOND = 60; const int MS_PER_UPDATE = 1000 / UPDATES_PER_SECOND; const int MAX_FRAMESKIP = 5; Uint32 elapsedMS = 0; Uint32 startTime = SDL_GetTicks(); Uint32 nextGameTick = SDL_GetTicks(); int loops; int numUpdates = 0; int numFrames = 0; while(game->IsRunning()) { while(SDL_PollEvent(&e)) { //Exit the game if the user hits the 'X' button on the window. if(e.type == SDL_QUIT) game->SetIsRunning(false); } //Game update, render loop loops = 0; while(SDL_GetTicks() > nextGameTick && loops < MAX_FRAMESKIP) { game->Update(); nextGameTick += MS_PER_UPDATE; loops++; ++numUpdates; } //Render the current frame game->Render(); ++numFrames; //Used to display FPS/UPS elapsedMS = SDL_GetTicks() - startTime; if(elapsedMS % 1000 == 0) { //Only update once per second game->FPS = numFrames / (elapsedMS / 1000.0); game->UPS = numUpdates / (elapsedMS / 1000.0); } } // End main loop So basically, here are my questions:   1) Is this an efficient game loop? By that I mean, will it update at about the same rate on all computers? It's designed to update the logic at 60 times per second no matter what the computer (usually), but the frame rate will change.   2) Is 60 updates per second too much? Too little?   3) How much should I look into capping the frame rate?   Let me know if you guys see anything you think I should change. I've been wracking my brain over this for 48 hours now, i'm ready to move on! :P Thanks guys!  
  9. Hey everyone! Before you continue, no, this isn't a post about getting MonoGame to work with C++!   Now that everyone didn't run away, I have a question that i've been debating for a few days now. I've been programming for 4-5 years now, and the entire time I have had game programming in my sights. Only recently I have been able to start development on a full game, not a sample game you make from a book where the game runs through and ends. I mean a full game, like one you would buy. I've been working on it for a couple weeks using XNA 4.0, even though I have been aware that XNA is basically dead in Microsofts eyes. I was using this as a learning experience, and I did learn a lot.   Now here comes the question. I have two options here to continue, because I feel like as though I know i'm learning, continuing with XNA is counter productive if I were to want to deploy and sell my games (key word, IF. I know not everyone is going to be able to, or want to deploy their games to sell). I could port my game over to MonoGame, which I looked through and scoured the internet for info on. The problem here, is that since it's still in development, there is no content pipeline that you get with XNA, which was a huge part of it. I know MonoGame is basically the go-to thing for XNA devs as well.   My other option is to switch to C++. I did what every hobby dev does, and started my tenure of programming trying to learn C++, so I do have SOME experience. (Some = getting a sprite to move with DirectX after I learned the language itself of course). Another thing to keep in mind is that I do all of this by myself, and do not work in a team.   More thinking about the future, would it be worth it to port my game to MonoGame, continue developing it, and deal with all the tricky workarounds that MonoGame has as of now, (Version 3.0 or 3.1, can't remember which), or would it be better to just start learning C++ again, and get back into that for game development?     Before everyone comes out with the, "no language is right for every situation, choose what works for you, etc", i'm not looking for advice on a personal level. I'm looking for advice at an industry level. Basically what i'm asking is, would it be worth it for a one man programming team to deal with the MonoGame stuff, or would it be worth it to make the switch back to C++? Taking the time to learn the language isn't a problem (I'm a third year student at college, I have some time before the real world!) What would be more advantageous in the long term is what I want to know.    I'm only looking for opinions here, as I am aware of how many factors can affect a decision like this. I'm not looking for anyone to tell me how hard one is, or how easy another is, as i've had at least some experience in both. Not saying i'm a pro, far from it actually. Just stuck in this tough decision that I can't figure out which side to go with! If it helps, I only work in 2D. Not really working on any 3D games, at least in the foreseeable future. Thanks everyone!
  10. Well, aside from the sprite programming, I have the DirectX code set aside to reuse. What I was mainly asking though, which I see may have been a bit confusing, was that I was looking for advice on how to just make what I have so far cleaner, and maybe a better way to put all the sprite stuff into one class. I realize that there are many ways to do things, but I was looking for opinions on how to just make this code cleaner.
  11. Hey guys, i'm just starting some actual programming with DirectX, and I was hoping someone could go over my code so far and tell me how well it's organized, and how well I could fix it. Thanks! All the program does so far is display a sprite that moves itself across the screen. [spoiler][code]//main source code file. contains windows api code #include <d3d9.h> #include <d3dx9.h> #include <ctime> #include <cstdio> #include "dxgraphics.h" #include "game.h" //window event callback function LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY: //release the DIrect3D Device if(d3ddev != NULL) d3ddev->Release(); //release the direct3d object' if(d3d != NULL) d3d->Release(); //call the "font end" shutdown function gameEnd(hWnd); //tell windows to kill this program PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); } //helper function to set up the window properties ATOM MyRegisterClass(HINSTANCE hInstance) { //create the window class structure WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); //fill the struct with info = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = GAMETITLE; wc.hIconSm = NULL; //set up the window with the class info return RegisterClassEx(&wc); } //entry point for a windows program int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hWnd; //register the class MyRegisterClass(hInstance); //set up the screen in windowed or fullscreen mode? DWORD style; if(FULLSCREEN) style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP; else style = WS_OVERLAPPEDWINDOW; //create a new window hWnd = CreateWindow( GAMETITLE, //window class GAMETITLE, //title bar style, //windows style CW_USEDEFAULT, // x position of the window CW_USEDEFAULT, //y positin of the window 1280, //width 720, //height of the window NULL, //parent window NULL, //menu hInstance, //application instance NULL ); // window parameters //was there an error creating the window? if(!hWnd) return false; //display the window ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); if(!Init_Direct3D(hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN)) return 0; //initialize the game if(!gameInit(hWnd)) { MessageBoxA(hWnd, "Error initializing game", "Error", MB_OK); return 0; } //main message loop int done = 0; while(!done) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { //look for quit message if(msg.message == WM_QUIT) done = 1; //decode and pass messages on to WndProc TranslateMessage(&msg); DispatchMessage(&msg); } else //process game loop (else prevents from running after window is closed) gameRun(hWnd); } return msg.wParam; }[/code] /GAME.h [code]#ifndef GAME_H #define GAME_H #include <d3d9.h> #include <d3dx9.h> #include <d3dx9math.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #include "dxgraphics.h" #include "sprite.h" #include "player.h" //application title #define GAMETITLE "Zombie Game" //screen set up #define FULLSCREEN 0 //0 = windowed, 1 = fullscreen #define SCREEN_WIDTH 1280 #define SCREEN_HEIGHT 720 //function prototypes int gameInit(HWND); void gameRun(HWND); void gameEnd(HWND); #endif[/code] //GAME.CPP [code]#include "game.h" #include "sprite.h" //Sprite Texture Objects (Holds full sprite sheet bmp) LPDIRECT3DTEXTURE9 main_character = NULL; //Sprite sheet for main character SPRITE mainCharacter; LPDIRECT3DSURFACE9 back; LPD3DXSPRITE sprite_handler; HRESULT result; //timing variable long start = GetTickCount(); //initialize the game int gameInit(HWND hwnd) { //set random number seed srand(time(NULL)); //create sprite handler object result = D3DXCreateSprite(d3ddev, &sprite_handler); if(result != D3D_OK) return 0; //load the main character sprite sheet main_character = LoadTexture("mainCharacter.bmp", D3DCOLOR_XRGB(255, 0, 255)); if(main_character == NULL) return 0; back = LoadSurface("background.bmp", NULL); //initializes the sprites properties mainCharacter.x = 100; mainCharacter.y = 150; mainCharacter.width = 64; mainCharacter.height = 64; mainCharacter.curframe = 0; mainCharacter.lastframe = 1; mainCharacter.animdelay = 8; mainCharacter.animcount = 0; mainCharacter.movex = 5; mainCharacter.movey = 0; return 1; } //the main game loop void gameRun(HWND hwnd) { //make sure the d3d device is valid if(d3ddev == NULL) return; //after short delay, ready for next frame? //this keeps the game running at a steady frame rate if(GetTickCount() - start >= 30) { //reset timing start = GetTickCount(); //move the sprite mainCharacter.x += mainCharacter.movex; mainCharacter.y += mainCharacter.movey; //warp the sprite on the edges of the screen if(mainCharacter.x > SCREEN_WIDTH - mainCharacter.width) mainCharacter.x = 0; if(mainCharacter.x < 0) mainCharacter.x = SCREEN_WIDTH - mainCharacter.width; //has animation delay reached threshold? if(++mainCharacter.animcount > mainCharacter.animdelay) { //reset counter mainCharacter.animcount = 0; //animate the sprite if(++mainCharacter.curframe > mainCharacter.lastframe) mainCharacter.curframe = 0; } } //start rendering if(d3ddev->BeginScene()) { //erase the entire backgoround d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE); //start sprite handler sprite_handler->Begin(D3DXSPRITE_ALPHABLEND); //create vector to update sprite position D3DXVECTOR3 position((float)mainCharacter.x, (float)mainCharacter.y, 0); //configure the rect for the source file RECT srcRect; int columns = 2; srcRect.left = (mainCharacter.curframe % columns) * mainCharacter.width; = (mainCharacter.curframe / columns) * mainCharacter.height; srcRect.right = srcRect.left + mainCharacter.width; srcRect.bottom = + mainCharacter.height; //draw the sprite sprite_handler->Draw( main_character, &srcRect, NULL, &position, D3DCOLOR_XRGB(255, 255, 255)); //stop drawing sprite_handler->End(); //stop rendering d3ddev->EndScene(); } //display the back buffer on the screen d3ddev->Present(NULL, NULL, NULL, NULL); } void gameEnd(HWND hwnd) { if(back != NULL) back->Release(); if(sprite_handler != NULL) sprite_handler->Release(); if(main_character != NULL) main_character->Release(); }[/code] //SPRITE.H [code]#ifndef SPRITE_H #define SPRITE_H //sprite structure typedef struct { int x, y; int width, height; int movex, movey; int curframe, lastframe; int animdelay, animcount; } SPRITE; //function prototypes int Collision(SPRITE sprite1, SPRITE sprite2); //collision detection function #endif[/code] //SPRITE.CPP [code]#include "sprite.h" #include <Windows.h> #include <d3d9.h> //collision function. tests for collision int Collision(SPRITE sprite1, SPRITE sprite2) { RECT rect1; rect1.left = sprite1.x + 1; = sprite1.y + 1; rect1.right = sprite1.x + sprite1.width - 1; rect1.bottom = sprite1.y + sprite1.height - 1; RECT rect2; rect2.left = sprite2.x + 1; = sprite2.y + 1; rect2.right = sprite2.x + sprite2.width - 1; rect2.bottom = sprite2.y + sprite2.height -1; RECT dest; return IntersectRect(&dest, &rect1, &rect2); } [/code] //dxgraphics.h [code]#ifndef DXGRAPHICS_H #define DXGRAPHICS_H //function prototypes int Init_Direct3D(HWND, int, int, int); LPDIRECT3DSURFACE9 LoadSurface(char*, D3DCOLOR); LPDIRECT3DTEXTURE9 LoadTexture(char*, D3DCOLOR); //variable declarations extern LPDIRECT3D9 d3d; extern LPDIRECT3DDEVICE9 d3ddev; extern LPDIRECT3DSURFACE9 backbuffer; #endif[/code] //DXGRAPHICS.CPP [code]#include <d3d9.h> #include <d3dx9.h> #include "dxgraphics.h" //variable declarations LPDIRECT3D9 d3d = NULL; LPDIRECT3DDEVICE9 d3ddev = NULL; LPDIRECT3DSURFACE9 backbuffer = NULL; int Init_Direct3D(HWND hwnd, int width, int height, int fullscreen) { //initialize direct3d d3d = Direct3DCreate9(D3D_SDK_VERSION); if(d3d == NULL) { MessageBoxA(hwnd, "Error init-ing Direct3D", "Error", MB_OK); return 0; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = (!fullscreen); d3dpp.SwapEffect = D3DSWAPEFFECT_COPY; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = width; d3dpp.BackBufferHeight = height; d3dpp.hDeviceWindow = hwnd; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //create the Direct3D device d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); if(d3ddev == NULL) { MessageBoxA(hwnd, "Error creating Direct3D device", "Error", MB_OK); return 0; } //clear the back buffer to black d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); //create pointer to the back buffer d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); return 1; } LPDIRECT3DSURFACE9 LoadSurface(char* filename, D3DCOLOR transcolor) { LPDIRECT3DSURFACE9 image = NULL; D3DXIMAGE_INFO info; HRESULT result; //get width and height from bitmap file result = D3DXGetImageInfoFromFile(filename, &info); if(result != D3D_OK) return NULL; //create surface result = d3ddev->CreateOffscreenPlainSurface( info.Width, //width of the surface info.Height, //height of the surface D3DFMT_X8R8G8B8, //surface format D3DPOOL_DEFAULT, //memory pool to use &image, //pointer to the surface NULL); //reserved(always NULL) if(result != D3D_OK) return NULL; //load surface from file into newly created surface result = D3DXLoadSurfaceFromFile( image, //destination surface NULL, //destionation pallatte NULL, //destionation rectangle filename, //source filename NULL, //source rectangle D3DX_DEFAULT, //controls how the image is filtered transcolor, //for transparency (0 for none) NULL); //source mage info(USUALLY NULL) //make sure the file was loaded okay if(result != D3D_OK) return NULL; return image; } LPDIRECT3DTEXTURE9 LoadTexture(char *filename, D3DCOLOR transcolor) { //the texture poitner LPDIRECT3DTEXTURE9 texture = NULL; //the struct for reading bitmap file info D3DXIMAGE_INFO info; //standard Windows return value HRESULT result; //get width and height from bitmap file result = D3DXGetImageInfoFromFile(filename, &info); if(result != D3D_OK) return NULL; //create the new texture by loading a bitmap image file D3DXCreateTextureFromFileEx( d3ddev, //Drect3D device object filename, //bitmap filename info.Width, //bitmap image width info.Height, //bitmap image height 1, //mip map levels(1 for no chain) D3DPOOL_DEFAULT, // the type of surface (standard) D3DFMT_UNKNOWN, //surface format (default)' D3DPOOL_DEFAULT, //memory class for the texture D3DX_DEFAULT, //image filter D3DX_DEFAULT, //mip filter transcolor, //color key for transparency &info, //bitmap file info NULL, //color palatte &texture); //destination texture //make sure the bitmap texture was loaded correctly if(result != D3D_OK) return NULL; return texture; }[/code][/spoiler]
  12. Need help finding books

    I found Game Programming All In One, i'm giving that a shot now. Other than that, what resources are worth using to learn game programming? I'll probably look for tutorials on whatever library i use, which will probably be DirectX. Any resources that can teach me some game programming and/or DirectX tutorials would be awesome
  13. Need help finding books

    It's not that it's like, syntax error. Well, it is. But not standard C++. It's usually Windows programming errors, and DirectX errors, as the book uses DIrectX as well. I think it usually has to do with being outdated. I believe he uses VS2005 in the book, and i'm using 2010. And he uses DirectX 9 but I had downloaded DirectX 10. I know how to solve most problems in code, but those errors were just too deep, going into Windows.h and other header files.
  14. Hey everyone, I was hoping you all could help me. I've been trying to find a good book to teach game programming, specifically 2D games. I've been looking through books and the closest one I found was Beginning Game Programming by Jonathan Harbour. The only thing about this book is I haven't had a source code compile without some kind of error. Even copy and paste doesn't take the errors out, and they are such complicated errors that it's beyond what I can do right now. All the other books seem to focus on 3D gaming. I'm hoping you guys have some good resources on learning 2D game programming. I have a basic understanding of C++, but i'm no expert. If anyone can point me in the direction of a good book or two for 2D game programming, that would be greatly appreciated! I think I might continue reading Game Coding Complete, but I think that also is mostly 3D.
  15. Where to go?

    Alright, I'll start working through the SMFL tutorials today! Appreciate the help like you wouldn't believe.