Jump to content

  • Log In with Google      Sign In   
  • Create Account


That One Guy

Member Since 27 Sep 2010
Offline Last Active Jan 03 2012 12:34 AM
-----

Topics I've Started

Microsoft Windows, SDL, and closing a Window @_@;

29 December 2011 - 12:56 PM

Hi... After having to install Windows 7, I've set up Codeblocks with MinGW and SDL. Now, then, I've been trying to get some things working. I've gotten a window open (just a simple, stupid test program) and can keep it open, but it won't close unless I use Task Manager to force it shut. I've tried everything I could think of and I never had this particular problem on Linux.

It closes on its own, like any other window, if I don't have a loop to keep it open, but with the loop? Complete and total failure to terminate.

#include "SDL.h"
#include <stdio.h>
#include <stdlib.h>
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
SDL_Surface *window;
union SDL_Event event;
int main(int argc,char *argv[]){
  if(SDL_Init(SDL_INIT_VIDEO)!=0){
	printf("Unable to initialise SDL: %s\n",SDL_GetError());
	  return 1;
	  atexit(SDL_Quit);
	return 1;
  }
  SDL_WM_SetCaption("Windows SDL Test",NULL);
  window=SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT,32,SDL_DOUBLEBUF);
  if(window==NULL){
	printf("Unable to set video mode: %s\n",SDL_GetError());
	return 1;
  }
  while(1){
	while(SDL_WaitEvent(&event)==0){
	  switch(event.type){
		case SDL_QUIT:
		SDL_Quit();
	  }
	}
  }
}

As with other code I've shown on this site, there are things I wouldn't want to do in a program I plan to release. I can't move onto the next bit of SDL testing on Windows until I get this issue resolved, though, and searching for hours has revealed nothing helpful. The outer loop is there because, without it, the window closes instantly.

SDL text-entry issues: The Sequel

16 December 2011 - 06:16 PM

Well... This is lame. I can't quite get this input thing working properly (second time having tried input, if I still had my first source I'd only have the backspace and uppercase to worry about). Basically, text won't appear unless you hit backspace several times (didn't act like that the first time), and due to the unicode stuff, I don't have access to shift-uppercase. What I'm trying to accomplish is a backspace that works (instead of turning things into t's or other letters) and to have the user's input always be uppercase.

Here's my unpleasant-to-the-eye sourcecode, please be gentle. @_@ I'm including all of what is in this file, just in case:
#include "/home/mdeans/Fiendlight/plyr.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_thread.h"
#include "SDL/SDL_ttf.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
SDL_Surface *surface[34];  ///0 = window, 1 = frame, 2-10 = lower box's text, 11-20 = upper box's text
SDL_Event event;
SDL_Rect src,dest;
SDL_Color font_color;
TTF_Font* mt_font;
char tr_panel[40];
char br_panel[20];
char user_input[9];
void plyr_talk();
void plyr_attk();
void plyr_ztat();
void plyr_get();
void plyr_look();
void plyr_warp(int how,int where[3]);
int input_slot;
int game_start(),game_load(),game_credits(),game_field(),game_battle();
int main(){
  if(SDL_Init(SDL_INIT_VIDEO)!=0){
	printf("Unable to initialise SDL:  %s\n", SDL_GetError());
	return 1;
  }
  else{
	SDL_EnableUNICODE(SDL_ENABLE);
	if(TTF_Init()!=0){
	  printf("Unable to initialise SDL_TTF:  %s\n", SDL_GetError());
	  return 1;
	}
	else if(TTF_Init()==0){
	  mt_font = TTF_OpenFont("gfx/FreeMono.ttf",16);
	  if(mt_font==NULL){
		printf("Unable to load font:  %s:  %s",mt_font,TTF_GetError());
	  }
	}
	SDL_WM_SetCaption("Fiendlight",NULL);
	surface[0] = SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT,32,SDL_DOUBLEBUF);
	if(surface[0]==NULL){
	  printf("Unable to set video mode:  %s\n", SDL_GetError());
	  return 1;
	}
	surface[1] = IMG_Load("gfx/frame.png");
	SDL_Color font_color = {180,180,180};
	surface[2] = TTF_RenderText_Solid(mt_font,"[S]tart new quest",font_color);
	surface[3] = TTF_RenderText_Solid(mt_font,"[C]ontinue your journey",font_color);
	surface[4] = TTF_RenderText_Solid(mt_font,"[V]iew the credits",font_color);
	src.x = 0;
	src.y = 0;
	src.h = surface[1]->h;
	src.w = surface[1]->w;
	dest.h = 480;
	dest.w = 640;
	dest.x = 0;
	dest.y = 0;
	SDL_FillRect(surface[0],NULL,SDL_MapRGBA(surface[0]->format,0,0,0,255));
	SDL_BlitSurface(surface[1], &src, surface[0], &dest);
	dest.x = 410;
	dest.y = 280;
	SDL_BlitSurface(surface[2], &src, surface[0], &dest);
	dest.y = 296;
	SDL_BlitSurface(surface[3], &src, surface[0], &dest);
	dest.y = 312;
	SDL_BlitSurface(surface[4], &src, surface[0], &dest);
	SDL_Flip(surface[0]);
	//while(1){
	  while (SDL_WaitEvent(&event) !=0){
		switch (event.type) {
		case SDL_QUIT:
		  exit(0);
		case SDL_KEYDOWN:
		  if(event.key.keysym.sym == SDLK_s){
			game_start();
		  }
		  else if(event.key.keysym.sym == SDLK_c){
			///game_load();
		  }
		  else if(event.key.keysym.sym == SDLK_v){
			///game_credits();
		  }
		}
	  }
	//}
  }
}
game_start(){
  surface[1] = IMG_Load("gfx/frame.png");
  SDL_Color font_color = {180,180,180};
  surface[2] = TTF_RenderText_Solid(mt_font,"By what name shalt",font_color);
  surface[3] = TTF_RenderText_Solid(mt_font,"thou be known?",font_color);
  surface[4] = TTF_RenderText_Solid(mt_font,user_input,font_color);
  src.x = 0;
  src.y = 0;
  src.h = surface[1]->h;
  src.w = surface[1]->w;
  dest.h = 480;
  dest.w = 640;
  dest.x = 0;
  dest.y = 0;
  SDL_FillRect(surface[0],NULL,SDL_MapRGBA(surface[0]->format,0,0,0,255));
  SDL_BlitSurface(surface[1], &src, surface[0], &dest);
  dest.x = 414;
  dest.y = 280;
  SDL_BlitSurface(surface[2], &src, surface[0], &dest);
  dest.y = 296;
  SDL_BlitSurface(surface[3], &src, surface[0], &dest);
  dest.y = 312;
  SDL_BlitSurface(surface[4], &src, surface[0], &dest);
  SDL_Flip(surface[0]);
  input_slot = 0;
  while(1){
	while(SDL_WaitEvent(&event)!=0){
	  switch(event.type){
		case SDL_QUIT:
		exit(0);
		case SDL_KEYDOWN:
		if(event.key.keysym.sym == SDLK_RETURN){
		  printf(user_input);
		}
		else if(event.key.keysym.sym != SDLK_RETURN){
		  if(event.key.keysym.sym != SDLK_BACKSPACE){
			if(input_slot <= 8){
			  input_slot = input_slot + 1;
			}
			SDL_EnableUNICODE(SDL_ENABLE);
			user_input[input_slot] = event.key.keysym.unicode;
		  }
		  else if(event.key.keysym.sym == SDLK_BACKSPACE){
			user_input[input_slot] = "_";
			if(input_slot>=0){
			  input_slot = input_slot - 1;
			}
		  }
		}
		surface[1] = IMG_Load("gfx/frame.png");
		SDL_Color font_color = {180,180,180};
		surface[2] = TTF_RenderText_Solid(mt_font,"By what name shalt",font_color);
		surface[3] = TTF_RenderText_Solid(mt_font,"thou be known?",font_color);
		surface[4] = TTF_RenderText_Solid(mt_font,user_input,font_color);
		src.x = 0;
		src.y = 0;
		src.h = surface[1]->h;
		src.w = surface[1]->w;
		dest.h = 480;
		dest.w = 640;
		dest.x = 0;
		dest.y = 0;
		SDL_FillRect(surface[0],NULL,SDL_MapRGBA(surface[0]->format,0,0,0,255));
		SDL_BlitSurface(surface[1], &src, surface[0], &dest);
		dest.x = 414;
		dest.y = 280;
		SDL_BlitSurface(surface[2], &src, surface[0], &dest);
		dest.y = 296;
		SDL_BlitSurface(surface[3], &src, surface[0], &dest);
		dest.y = 312;
		SDL_BlitSurface(surface[4], &src, surface[0], &dest);
		SDL_Flip(surface[0]);
	  }
	}
  }
}

If you see any other ways I could improve this wreck, I'll accept suggestions, but I really just want to get backspace and caps working. I hope someone can and will help. Any assistance is greatly appreciated and will be (likely) included in my credits.
[EDIT: I notice that I kinda accidentally enabled unicode twice. Wasn't intentional.]

SDL_TTF text entry?

03 December 2011 - 01:38 PM

I'm in kind of a goofy scenario right now. Two things must be done before I can get back into working on this (Ultima-style) RPG I'm planning, and one of them is driving me loopy:
How might I go about allowing the player to enter text and /blit the text entered/ like a normal SDL_TTF surface? Must I really work this entire process out from scratch?
The simplest way I can invision doing it is by taking a keypress, adding it to my user_input variable, taking another and adding it to the next slot of the char array (if this is a bad idea in some way, let me know, but this isn't the question I'm asking) and continuing until the user presses 'enter' to finish. The way I'm asking about is probably impossible with plain SDL: Is it possible to simply take the input, adding it and blitting the surface with each keypress so that it enters (from the player's perspective) as simply as typing in a word processor? If not, I understand, I could go with my alternate plan (requiring a ROYALLY huge rewrite and replanning, but it is possible and I'd be willing if I had to do so). The first method I mentioned would have involved a separate if/else for each alphabetical key, and would result in tremendous tedium, but I can do that if need be, also. I'm probably horribly wrong about these methods and there might be a simpler way, so I'm asking: Could someone lend me a little advice, or a tutorial link/etc? Thanks to whomever can help! @_@;

EDIT: Just to be clear about this, I'm using C.

PARTNERS