Can any one teach me how to initialise the mouse and create small games. I know basic things like drawing a rectangle etc.

Dani AI

Generated

Good question, . and are right that you quickly run into OS/API details, but you can learn mouse-driven game basics in plain C without diving straight into low-level Win32 or graphics APIs. A practical path is: pick a small multimedia library (SDL2 or Allegro), write a simple event loop, handle mouse events, update a tiny game state, then render. That keeps the focus on game logic instead of windowing quirks.

Start small and iterate:

  • Install the library and compile sample programs so you know the build step works.
  • Create one window and an event loop. Process mouse events (move, button down/up) and map coordinates to your game objects.
  • Implement a single mechanic first (for example: click a rectangle to "collect" it, or drag to move it). That forces you to handle hit tests, button state, and simple rendering.
  • Add timing (fixed timestep or simple frame cap) and polish once the interaction is solid.

Minimal SDL2 example (C): a window, event loop, mouse motion + left-click detection and a rectangle you can click or follow the cursor.

#include <SDL2/SDL.h>
#include <stdio.h>

int main(void){
    if (SDL_Init(SDL_INIT_VIDEO) != 0) { fprintf(stderr, "SDL_Init: %s\n", SDL_GetError()); return 1; }
    SDL_Window *w = SDL_CreateWindow("mouse demo", 100,100,640,480,0);
    SDL_Renderer *r = SDL_CreateRenderer(w,-1,SDL_RENDERER_ACCELERATED);
    SDL_Rect box = {280,200,80,60};
    int run=1;
    while(run){
        SDL_Event e;
        while(SDL_PollEvent(&e)){
            if(e.type==SDL_QUIT) run=0;
            else if(e.type==SDL_MOUSEMOTION){ box.x = e.motion.x - box.w/2; box.y = e.motion.y - box.h/2; }
            else if(e.type==SDL_MOUSEBUTTONDOWN && e.button.button==SDL_BUTTON_LEFT){
                int x=e.button.x,y=e.button.y;
                if(x>=box.x && x<box.x+box.w && y>=box.y && y<box.y+box.h){
                    /* clicked the box: trigger action */
                }
            }
        }
        SDL_SetRenderDrawColor(r,30,30,30,255); SDL_RenderClear(r);
        SDL_SetRenderDrawColor(r,200,60,60,255); SDL_RenderFillRect(r,&box);
        SDL_RenderPresent(r); SDL_Delay(16);
    }
    SDL_DestroyRenderer(r); SDL_DestroyWindow(w); SDL_Quit(); return 0;
}

Compile/test tip: on Unix-like systems use gcc -o mouse_test mouse_test.c $(sdl2-config --cflags --libs). If events seem missing, check window focus, return values from init calls, and platform DPI/scaling. After this, expand to sprite drawing, collision, and a small project (click-to-move, dragging, or a simple shooter) to cement the concepts.

Recommended Answers

All 2 Replies

hello everyone,
Creating games in c++ is more than just initialising the mouse. It involves alot of win api and specialized know how. Maybe you should try game programming with a basic dialect first as it is easier to understand and maybe even if you like try java and no i am not telling you to give up game programming using c++.
What i am saying is that is better for you to get some alogorthim experience.
You can try dark basic, i hear its quite good but if you use it let me know how you find it. Well this just my two cents.

Thank You

Yours Sincerely

Richard West

True, even simple games are not something everyone can wip up ya know.

If you really want to make games,then you must know the lang you use quite well,also you should know some API like OpenGL or DirectX if you want to do GUI,window based games without going down to rasterization (drawing your on lines,polygons etc).Else you will want to learn the Windows GUI API.

Or you can go in for DOS console based games using Mode13h or ModeX.

So much for displaying wath you want.The rest of the stuff like internals of the game are up to you and your skill.If you think logically you can make games quite easilly.The best step is to first plan out everything before you actully start coding.

Belive me, and also start out with the simplest game you can think of.

http://www.gamedev.net/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.